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))
`shutil` 模块是 Python 中用于对文件和文件夹进行操作的标准库之一。它提供了一些简单而又强大的函数,使得文件和文件夹的复制、移动、删除等操作变得非常容易。 下面是一些常见的 `shutil` 模块中的函数及其用法: 1. `shutil.copy(src, dst)`:将文件从源路径复制到目标路径。如果目标路径是一个文件,则覆盖该文件;如果目标路径是一个目录,则在该目录下创建同名文件并复制。 ```python import shutil src_path = "source_file.txt" dst_path = "destination_file.txt" shutil.copy(src_path, dst_path) ``` 2. `shutil.copy2(src, dst)`:与 `shutil.copy()` 类似,但还会复制文件的元数据(如创建时间、修改时间等)。 ```python import shutil src_path = "source_file.txt" dst_path = "destination_file.txt" shutil.copy2(src_path, dst_path) ``` 3. `shutil.move(src, dst)`:将文件或目录从源路径移动到目标路径。如果目标路径是一个文件,则覆盖该文件;如果目标路径是一个目录,则在该目录下创建同名文件或目录并移动。 ```python import shutil src_path = "source_file.txt" dst_path = "destination_file.txt" shutil.move(src_path, dst_path) ``` 4. `shutil.rmtree(path)`:递归删除一个目录及其内容。 ```python import shutil dir_path = "directory_to_delete" shutil.rmtree(dir_path) ``` 5. `shutil.make_archive(base_name, format, root_dir)`:创建一个压缩包,并返回压缩包的文件路径。`base_name` 是压缩包的文件名,`format` 是压缩包的格式(如 "zip"、"tar" 等),`root_dir` 是要打包的根目录。 ```python import shutil dir_to_archive = "directory_to_archive" archive_name = "archive" archive_format = "zip" shutil.make_archive(archive_name, archive_format, dir_to_archive) ``` 以上是 `shutil` 模块中常用的一些函数,更多函数可以查看 Python 官方文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值