本文實例為大家分享了python實現遍歷文件夾修改文件后綴的具體代碼,供大家參考,具體內容如下
用法 python Modifer.py ./ -fp java xml
# coding:utf-8 # Build by LandGrey 2016-05-27 import os import sys #批量修改路徑與文件名 def Modifyprefix(Path,oldcontent,newcontent): all_file_list = os.listdir(Path) #列出指定目錄下的所有文件 for file_name in all_file_list: currentdir =os.path.join(Path, file_name) #連接指定的路徑和文件名or文件夾名字 if os.path.isdir(currentdir): #如果當前路徑是文件夾,則跳過 Modifyprefix(currentdir,oldcontent,newcontent) fname = os.path.splitext(file_name)[0] #分解出當前的文件路徑名字 ftype = os.path.splitext(file_name)[1] #分解出當前的文件擴展名 if oldcontent in fname: fdcount[0]+=1 replname =fname.replace(oldcontent,newcontent) #將原文件名中的'oldcontent'字符串內容全替換為'newcontent'字符串內容 newname = os.path.join(Path,replname+ftype) #文件路徑與新的文件名字+原來的擴展名 os.rename(currentdir,newname) #重命名 #批量修改文件擴展名(后綴) def Modifypostfix(Path,oldftype,newftype): all_file_list = os.listdir(Path) #列出指定目錄下的所有文件 for file_name in all_file_list: currentdir =os.path.join(Path,file_name) if os.path.isdir(currentdir): #迭代 Modifypostfix(currentdir,oldftype,newftype) fname = os.path.splitext(file_name)[0] ftype = os.path.splitext(file_name)[1] if oldftype in ftype[1:]: #找到需要修改的擴展名 typecount[0]+=1 ftype=ftype.replace(oldftype,newftype) newname = os.path.join(Path,fname+ftype) #文件路徑與原來的文件名字+新的擴展名 os.rename(currentdir,newname) #重命名 def Useage(): print unicode("\n[+] 用法: python Modifer.py [指定目錄] [選項] [參數1] [參數2]","utf-8") print unicode("[+] 選項 [-fd] :批量修改目錄和文件名 [參數1]: 需要替換的字符 [參數2]:替換字符串","utf-8") print unicode("[+] 選項 [-fp] :批量修改文件后綴名 [參數1]: 原后綴名 [參數2]:需要替換的后綴名","utf-8") print unicode("[+] 選項 [-all] :批量修改目錄、文件名和文件后綴名 [參數1]: 需要替換的字符部分 [參數2]:替換字符串","utf-8") print unicode(r"[+] 用法示例:python Modifer.py D:\files -fp txt data","utf-8") if __name__=="__main__": typecount=[0] fdcount=[0] if len(sys.argv)==2 and "-h" in sys.argv[1]: Useage() sys.exit() elif len(sys.argv) !=5: print unicode("\n[+] 參數錯誤 !\n","utf-8") print unicode("[+] 用 -h 或--help 參數查看Modifer.py用法","utf-8") sys.exit() elif os.path.isdir(sys.argv[1]) is False: print unicode("\n[+] 指定目錄錯誤 ! 請檢查輸入路徑是否正確,路徑中不能有空格\n","utf-8") print unicode("[+] 用 -h 或--help 參數查看Modifer.py用法","utf-8") sys.exit() elif sys.argv[2]=="-fd": Modifyprefix(sys.argv[1],sys.argv[3],sys.argv[4]) print unicode("\n[+] Modifer.py Build by LandGrey","utf-8") print unicode("[+] 完成 !","utf-8") print unicode("[+] 共修改%s個目錄和文件名"%fdcount[0],"utf-8") elif sys.argv[2]=="-fp": Modifypostfix(sys.argv[1],sys.argv[3],sys.argv[4]) print unicode("\n[+] Modifer.py Build by LandGrey","utf-8") print unicode("[+] 完成 !","utf-8") print unicode("[+] 共修改%s個后綴名"%typecount[0],"utf-8") elif sys.argv[2]=="-all": Modifypostfix(sys.argv[1],sys.argv[3],sys.argv[4]) Modifyprefix(sys.argv[1],sys.argv[3],sys.argv[4]) print unicode("\n[+] Modifer.py Build by LandGrey","utf-8") print unicode("[+] 完成 !","utf-8") print unicode("[+] 共修改%s個目錄名、文件名和后綴名"%(typecount[0]+fdcount[0]),"utf-8") else: print unicode("\n[+] 選項錯誤 !\n","utf-8") print unicode("[+] 用 -h 或--help 參數查看Modifer.py用法","utf-8") sys.exit()
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
