Python批量重命名 工具贴(一)
说明
- 由于在处理图片数据和其他数据时,经常需要对数据进行批量重命名操作,每次使用时都需要重写,非常不便,因此记录下重命名代码方便后续使用。
文件结构说明
参数说明:
- path为输入路径
- image_type为重命名文件类型
- file_01, file_02...为重命名的文件
代码部分
# 批量重命名
# 2020-12-10
import os
def rename(filepath, image_type):
'''
对filepath下的文件进行重命名
:param filepath: 存放重命名文件的文件夹路径
:param image_type: 重命名图像格式类型
:return: None
'''
file = os.listdir(filepath)
file.sort()
counter = 1
for image in file:
old_name = filepath + '\\' + image
new_name = filepath + '\\' + '%08d' % counter + image_type
counter += 1
os.rename(old_name, new_name)
print(old_name + '=======>' + new_name)
def pichuli(path, image_type):
'''
对path文件夹下的文件夹下的文件进行重命名批量处理
:param path: 批处理的文件夹路径
:param image_type: 重命名图像格式类型
:return: None
'''
file = os.listdir(path)
file.sort()
for sub_path in file:
filepath = path + '\\' + sub_path
print('\n正在处理' + filepath + '下的文件...')
rename(filepath, image_type)
print('done!')
if __name__ == "__main__":
param = {'path' : r'C:\Users\HP\Desktop\standing',
'image_type' : '.png'
}
pichuli(**param)