栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python copy/delete 同名图片

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python copy/delete 同名图片

import os
import shutil
import os.path as osp

chosen_path = "/选择图片路径/"
new_path = "/生成图片路径/"
os.makedirs(new_path, exist_ok=True)
all_path = "/总的图片路径/"

index = 0
###copy/delete 单级目录
# 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:
        #print("chosen_name",chosen_name)
        file_dir_path = root_chosen_path.split("/")[-1]       
        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_:
                if file_gt_name == all_name:
                    print("chosen_name###################################:", chosen_name)
                    print("chosen_img_path:", chosen_img_path)
                    print("file_dir_path:", file_dir_path)
                    print("file_gt_name:",file_gt_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_path)
                    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))
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/822079.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号