指定したファイル名またはフォルダー内のコンテンツを指定したファイルにコピーします -- 例: 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