import os
import shutil
import os.path as osp
#dir_path = "./data/images/"
chosen_path = "/被选图片文件夹B路径/"
new_path = "/复制到新文件夹文件夹C路径/images/"
#new_path = "./data/select/"
os.makedirs(new_path, exist_ok=True)
all_path = "/所有图片文件夹A路径/src_split/"
index = 0
###copy
# for files in os.listdir(chosen_path):
# filesa = os.path.splitext(files)[0]
# #print("filesa:"+filesa)
# filesa = (filesa + ".jpg")
# for m in os.listdir(all_path):
# # print("filesa", str(filesa))
# # print("m", str(m))
# if str(m) == str(filesa):
# print("filesa:", filesa)
# print("m:", m)
# print(files)
# #shutil.copy(os.path.join(path,m),new_path)
# shutil.copy(os.path.join(all_path, m), new_path)
# #newp = os.path.join(new_path, m)
# #newp=os.path.join(new_path,m)
# #os.remove(os.path.join(dir_path, files))
# #print('成功删除文件:', os.path.join(dir_path, files))
# index = index + 1
# #print("copy %s to new path:"%(m) + newp)
# print("down 共复制%s张图"%(index))
###delete/copy
for root_chosen_path, dirs, files in os.walk(chosen_path):
print("root_chosen_path:", root_chosen_path)
# print("dirs:", dirs)
# print("files:", files)
for chosen_name in files:
file_dir = root_chosen_path.split("/")[-1]
chosen_name_prefix = chosen_name.split(".")[0]
# print("file_dir:",file_dir)
# print("chosen_name:",chosen_name)
# print("chosen_name_prefix:",chosen_name_prefix)
#file_gt_name = chosen_name.replace("jpg","png")
chosen_img_path = osp.join(root_chosen_path, chosen_name)
for root_all_path, dirs_, files_ in os.walk(all_path):
# print("root_all_path:", root_all_path)
#root_all_path_name = osp.split(root_all_path)[1].split("/")[-1]
for all_name in files_:
#Python startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。
if all_name.startswith(chosen_name_prefix):
#if file_gt_name == all_name:
print("chosen_name###################################:", chosen_name)
print("chosen_img_path:", chosen_img_path)
print("file_dir:", file_dir)
print("all_name:",all_name)
all_img_path = osp.join(root_all_path, all_name)
#print("all_img_path:", all_img_path)
#print("root_all_path_name:", root_all_path_name)
new_path_copy = osp.join(new_path, file_dir)
os.makedirs(new_path_copy, exist_ok=True)
shutil.copy(all_img_path, new_path_copy)
# os.remove(all_img_path)
# print('成功删除文件:', all_img_path)
print("copy %s to new path:"%(all_img_path) + new_path_copy)
index = index + 1
else:
continue
print("done!, a total of %s picture are copyed."%(index))
#按帧删除图片
# for files in os.listdir(dir_path):
# filesa = os.path.splitext(files)[0]
# print(filesa)
# frame = filesa.split('_')[-1]
# print(int(frame))
# remainder = int(frame) % 25
# print(remainder)
# if remainder == 0:
# os.remove(os.path.join(dir_path, files))
# index = index + 1
# print('成功删除文件:', files)
# print("done!, a total of %s picture are deleted."%(index))