python按顺序对图片批量重命名

python按顺序对图片批量重命名

import os

class BatchRename():

    def __init__(self):
        self.path = '/home/hitwh/test'

    def rename(self):
        filelist = os.listdir(self.path)
        filelist.sort()
        total_num = len(filelist)
        i = 0
        for item in filelist:
            if item.endswith('.png'):
                src = os.path.join(os.path.abspath(self.path), item)
                s = str(i)
                s = s.zfill(6)
                dst = os.path.join(os.path.abspath(self.path), s + '.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 jpgs' % (total_num, i))


if __name__ == '__main__':
    demo = BatchRename()
    demo.rename()

参考:python按文件顺序批量修改文件名

    原文作者:don't quit
    原文地址: https://blog.csdn.net/weixin_44354586/article/details/103784884
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞