复制文件夹内指定的文件名或者内容到指定文件---比如:json

下面是将文件夹下的json文件从0这个文件夹下复制到另一个0——json文件目录下,下面有好几个json是可以改的,代码当中。

import os
import shutil

path = r'C:\Users\Administrator\Desktop\project\fire_dataset_7_0\0'
new_path = r'C:\Users\Administrator\Desktop\project\fire_dataset_7_0\0_json'
filelist = os.listdir(path)
i = 1

for item in filelist:
    if item.endswith('json') or item.endswith('json') or item.endswith('json') or item.endswith('json'):
        src = os.path.join(os.path.abspath(path), item)
        dst = os.path.join(os.path.abspath(new_path),item)
        # new_name = os.path.join(os.path.abspath(new_path),''+str(i)+'.bmp')

        new_name = os.path.join(os.path.abspath(new_path), '' + str(i)+'.json')
       #复制图像
        shutil.copy(src,dst)
       #重命名---把下面两行注释了就不会更改图片的名字。。。
        os.rename(dst, new_name)
        i += 1

        print(src)
        print(new_name)


猜你喜欢

转载自blog.csdn.net/qq_44666320/article/details/126675544
今日推荐