时间:2020-10-09 22:41:46 | 栏目:Python代码 | 点击:次
//有1-22个文件夹,各文件夹下有Detect_0文件夹,此文件夹下有source与mask文件夹,目的是将需要获取图片的
文件夹下的图片复制到新的文件夹下并按顺序重命名
import os import shutil //删除之前文件夹并新建空文件夹 shutil.rmtree(r'E:\\all_project\\picture') os.makedirs("E:\\all_project\\picture\\source\\") os.makedirs("E:\\all_project\\picture\\mask\\") //确定需要获取图片的文件夹编号 # arr=["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22"] arr=["1","2"] source_num=0 mask_num=0 source_new = 1 mask_new = 1 for i in arr: source = 'E:\\all_project\\'+i+'\\Detect_0\\source\\' source_final = 'E:\\all_project\\picture\\source\\' source_ls = os.listdir(source) print(source_ls) mask = 'E:\\all_project\\'+i+'\\Detect_0\\mask\\' mask_final = 'E:\\all_project\\picture\\mask\\' mask_ls = os.listdir(mask) print(mask_ls) source_list = os.listdir(source) mask_list = os.listdir(mask) source_num += len(source_ls) mask_num += len(mask_ls) print(source_num) print(mask_num) old = 1 while len(os.listdir(source_final)) < source_num: oldname = source + str(old) + ".bmp" if os.path.exists(oldname): newname = source_final + str(source_new) + ".bmp" shutil.copyfile(oldname, newname) source_new += 1 print(source_new) old += 1 else: old += 1 old = 1 while len(os.listdir(mask_final)) < mask_num: oldname = mask + str(old) + ".bmp" if os.path.exists(oldname): newname = mask_final + str(mask_new) + ".bmp" shutil.copyfile(oldname, newname) mask_new += 1 old += 1 else: old += 1