python批量復制文件以及文件重命名


1.批量復制文件到新文件夾並重新按順序命名

復制文件要用到shutil.copy(),所以要下載shutil庫,到Anaconda Prompt輸入命令:pip install pytest-shutil

注意:其中zfill(6)的作用是將1變為6位數,缺少位置0代替,結果就是00001

 1 # -*- coding: utf-8 -*-  2 """  3 Created on Fri May 15 14:38:21 2020  4  5 @author: panq  6 """  7  8 import os  9 import shutil 10 11 def alter_fileName(path): # 獲取目錄下所有jpg格式文件的文件名 12 n=0 13 filelist= os.listdir(targe_path) 14 for i in filelist: #i是path目錄下的文件名 15 oldname=targe_path+os.sep+filelist[n]#oldname是原文件路徑 16 newname=targe_path+os.sep+str(n+1).zfill(6)+'.jpg'#newname是新文件名,路徑+新文件名 17 os.rename(oldname,newname) #改名字 18 print(oldname,'======>',newname) 19 n+=1 20 21 22 def file_copy(path,targe_path): #將path目錄下所有jpg文件復制到targe_path 23 ''' 24  root 所指的是當前正在遍歷的這個文件夾的本身的地址 25  dirs 是一個 list ,內容是該文件夾中所有的目錄的名字(不包括子目錄) 26  files 同樣是 list , 內容是該文件夾中所有的文件名(不包括子目錄) 27 ''' 28 for root,dirs,files in os.walk(path): 29 for name in files: 30 if name.endswith('.jpg'):#若文件名結尾是以jpg結尾,則復制到新文件夾 31 list=(os.path.join(root,name)) #list是jpg文件的全路徑 32 shutil.copy(list, targe_path) #將jpg文件復制到新文件夾 33 34 path="F:\任務1測試數據" 35 targe_path="F:\panqiaoyan" 36 file_copy(path, targe_path) 37 alter_fileName(targe_path)

 

 

2.使用 python 編寫一個自動程序,把所有的圖片提取出來,第 1類的放在一個新建文件夾名字“1”里面,第 2 類的放在一個新建文件夾名字“2”里 面 , 依 次 類 推 。 且 圖 片 需 要 重 新 命 名 , 第 1 類 的 圖 片 命 名 為100001,100002,……;第 2 類的圖片命名為 200001,200002,……;依次類推

注:1、2、3、4、5文件夾我已經建好了,你也可以程序自動創建。其中zfill(5)的作用是將1變為5位數,缺少位置0代替,結果就是0001

 1 # -*- coding: utf-8 -*-  2 """  3 Created on Sat May 16 17:53:44 2020  4  5 @author: panq  6 """  7 import os  8 import shutil  9 10 path="F:\任務2測試數據" 11 targe_path="F:\panqiaoyan01" 12 n1=0 #記錄第一類的個數 13 n2=0 14 n3=0 15 n4=0 16 n5=0 17 for root,dirs,files in os.walk(path): 18 for name in files: 19 if name.startswith("1"): #當照片是第一類時,復制到1文件夾 20 targe_path1="F:/panqiaoyan01/1" 21 list=(os.path.join(root,name)) #原文件路徑 22 shutil.copy(list, targe_path1) #將未改名的文件復制到新文件夾中 23 oldname=targe_path1+os.sep+name #復制之后的舊文件名(路徑+文件名) 24 newname=targe_path1+os.sep+"1"+str(n1+1).zfill(5)+'.jpg' #新文件名(路徑+文件名) 25 os.rename(oldname,newname) #到新文件夾處改文件名 26 n1+=1 27 if name.startswith("2"): 28 targe_path1="F:/panqiaoyan01/2" 29 list=(os.path.join(root,name)) 30 newname=targe_path1+os.sep+"2"+str(n2+1).zfill(5)+'.jpg' 31 oldname=targe_path1+os.sep+name 32  shutil.copy(list, targe_path1) 33  os.rename(oldname,newname) 34 n2+=1 35 if name.startswith("3"): 36 targe_path1="F:/panqiaoyan01/3" 37 list=(os.path.join(root,name)) 38 newname=targe_path1+os.sep+"3"+str(n3+1).zfill(5)+'.jpg' 39 oldname=targe_path1+os.sep+name 40  shutil.copy(list, targe_path1) 41  os.rename(oldname,newname) 42 n3+=1 43 if name.startswith("4"): 44 targe_path1="F:/panqiaoyan01/4" 45 list=(os.path.join(root,name)) 46 newname=targe_path1+os.sep+"4"+str(n4+1).zfill(5)+'.jpg' 47 oldname=targe_path1+os.sep+name 48  shutil.copy(list, targe_path1) 49  os.rename(oldname,newname) 50 n4+=1 51 if name.startswith("5"): 52 targe_path1="F:/panqiaoyan01/5" 53 list=(os.path.join(root,name)) 54 oldname=targe_path1+os.sep+name 55  shutil.copy(list, targe_path1) 56 newname=targe_path1+os.sep+"5"+str(n5+1).zfill(5)+'.jpg' 57  os.rename(oldname,newname) 58 n5+=1


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM