import osimport 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=0mask_num=0source_new = 1mask_new = 1for 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