文件重命名+python
来源:互联网 发布:免费hifi播放软件 编辑:程序博客网 时间:2024/06/14 21:01
该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()
阅读全文