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