Windows下对文件夹下所有图片批量重命名(附C++,python,matlab代码)
2018年12月16日
效果:
参考:Windows下对文件夹下所有图片批量重命名(附C++,python,matlab代码)
经修改过后的自用Python代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# -*- coding:utf8 -*- import os class BatchRename(): ''' 批量重命名文件夹中的图片文件 ''' def __init__(self): self.path = 'F:\labelImg\JPEGImages' def rename(self): filelist = os.listdir(self.path) total_num = len(filelist) i = 1 n = 6 for item in filelist: if item.endswith('.jpg'): n = 6 - len(str(i)) src = os.path.join(os.path.abspath(self.path), item) dst = os.path.join(os.path.abspath(self.path), str(0)*n + str(i) + '.jpg') try: os.rename(src, dst) print ('converting '+src+' to '+dst+' ...') i = i + 1 except: continue if __name__ == '__main__': demo = BatchRename() demo.rename() |