Copy the specified file name or content in the folder to the specified file---for example: json

The following is to copy the json file under the folder from the 0 folder to another 0——json file directory. There are several json files below that can be changed, in the code.

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)


Guess you like

Origin blog.csdn.net/qq_44666320/article/details/126675544