执行下列代码程序即可:
创建一个python文件 file_rename.py , 然后执行 python file_rename.py
# -*- coding: utf-8 -*-
import os
class BatchRename():
def __init__(self):
self.path = '/home/wsy/data/calibration/20220706/data_calibration' # 图片的路径
def rename(self):
filelist = os.listdir(self.path)
filelist.sort()
total_num = len(filelist) # 获取文件中有多少图片
i = 1 # 文件命名从哪里开始(即命名从哪里开始)
for item in filelist:
if item.endswith('.png'):
src = os.path.join(self.path, item)
dst = os.path.join(os.path.abspath(self.path),str(i)+ '.png')
try:
os.rename(src, dst)
print('converting %s to %s ...' % (src, dst))
i = i + 1
except Exception as e:
print(e)
print('rename dir fail\r\n')
print('total %d to rename & converted %d pngs' % (total_num, i))
if __name__ == '__main__':
demo = BatchRename() #创建对象
demo.rename() #调用对象的方法