该python代码可以批量更改图像文件名,更改文件名从1-totalnum,需注意文件名不能重复,否则程序中途停止不能更改全部文件。
import os
class BatchRename():
def init(self):
self.path = ‘E:\huang\My_project\iceb\no_ice’
def rename(self):
filelist = os.listdir(self.path) #文件夹下文件列表
total_num = len(filelist)
i = 1
for item in filelist:
src = os.path.join(os.path.abspath(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:
continue
print('total %d to rename & converted %d pngs' % (total_num, i))
if name == ‘main‘:
demo = BatchRename()
demo.rename()