python批量修改json文件

import json

import os

path = 'G:\Desktop\要修改文件的目录'

dirs = os.listdir(path)

num_flag = 0

for file in dirs:                            # 循环读取路径下的文件并筛选输出

    if os.path.splitext(file)[1] == ".json":  # 筛选csv文件

        num_flag = num_flag +1

        print(file , end=' ')

        # print(os.path.join(path,file))

        with open(os.path.join(path,file),'r') as load_f:

            load_dict = json.load(load_f)

        load_dict['shapes'][0]['label'] = 'lian_mu_dan_gao1'

        print(load_dict['shapes'][0]['label'])

        with open(os.path.join(path,file),'w') as dump_f:

            json.dump(load_dict, dump_f)

if(num_flag == 0):

    print('所选文件夹不存在json文件,请重新确认要选择的文件夹')

else:

    print('共{}个json文件'.format(num_flag))

你可能感兴趣的:(python批量修改json文件)