python修改目录下指定后缀的文件名

# -*- coding:utf8 -*-

import os
import sys
class BatchRename():

    def __init__(self):
        self.path = '/home/admin/img'

    def rename(self):
        filelist = os.listdir(self.path)
        total_num = len(filelist)
        i = 0
        for item in filelist:
            if item.endswith('.jpb'):
                src = os.path.join(os.path.abspath(self.path), item)
                dst = os.path.join(os.path.abspath(self.path), str(i)+'.jpb')#str(item).split('.')[0]+'ps' + '.jpg')
                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修改目录下指定后缀的文件名)