python将文件夹下的图片批量命名

-- coding:utf8 --

import os

class BatchRename():

定义函数执行图片的路径

def __init__(self):
    self.path =r'C:\Users\Administrator\Desktop\图片'

定义函数实现重命名操作

def rename(self):
    filelist = os.listdir(self.path)
    total_num = len(filelist)
    i = 101
    for item in filelist:
        if item.endswith('.png'):
            src = os.path.join(os.path.abspath(self.path), item)
            dst = os.path.join(os.path.abspath(self.path), str(i) + '.png')
            print(src)
            print(dst)
            try:
                os.rename(src, dst)
                print ('converting %s to %s ...'%(src, dst))
                i = i + 1
            except:
                continue
    print ('total %d to rename & converted %d jpgs' % (total_num, i))

主函数调用

if name == 'main':
demo = BatchRename()
demo.rename()

你可能感兴趣的:(python将文件夹下的图片批量命名)