rename原版+改进版

原版

import os

# this_file_path = __file__
# this_dir_path = os.path.dirname(this_file_path)
this_dir_path = './'
json_index = 433
png_index = 433
for file in os.listdir(this_dir_path):
    file_path = os.path.join(this_dir_path, file)
    if os.path.splitext(file_path)[-1] == '.png':
        new_file_path = '.'+'/'.join((os.path.splitext(file_path)[0].split('\\'))[:-1]) + '/{:0>4}_Color.png'.format(png_index)
        png_index += 1
        print(file_path+'---->'+new_file_path)
        os.rename(file_path, new_file_path)
    elif os.path.splitext(file_path)[-1] == '.json':
        new_file_path = '.'+'/'.join((os.path.splitext(file_path)[0].split('\\'))[:-1]) + '/{:0>4}_Color.json'.format(json_index)
        json_index += 1
        print(file_path+'---->'+new_file_path)
        os.rename(file_path,new_file_path)

改进版:

import os

# this_file_path = __file__
# this_dir_path = os.path.dirname(this_file_path)
this_dir_path = 'D:\视频照片资料\dataset\LAP\image3'
json_index = 433
png_index = 1046
for file in os.listdir(this_dir_path):
    file_path = os.path.join(this_dir_path, file)
    if os.path.splitext(file_path)[-1] == '.jpg':
        new_file_path = '.'+'/'.join((os.path.splitext(file_path)[1].split('\\'))[:-1]) + '/{:0>4}.jpg'.format(png_index)
        png_index += 1
        print(file_path+'---->'+new_file_path)
        os.rename(file_path, new_file_path)

输出的图像会放在当前程序代码下

路径:D:\vaaacomputer\Program\deep-learning-for-image-processing-master\datasetapi-master\datasetapi-master

你可能感兴趣的:(python,json,java,开发语言)