利用Python对文件进行批量重命名——以图片文件为例
效果如下:0001号用户的第 i 张图片
代码:
import os class ImageRename(): def __init__(self): self.path = 'C:/Users/lbpeng/Desktop/test/chictopia2/images1/fashioninmysoul/full' def rename(self): filelist = os.listdir(self.path) totalnum = len(filelist) i = 1 for item in filelist: if item.endswith('.jpg'): src = os.path.join(os.path.abspath(self.path), item) dst = os.path.join(os.path.abspath( self.path), '0001' + format(str(i), '0>3s') + '.jpg') os.rename(src, dst) i = i + 1 if __name__ == '__main__': newname = ImageRename() newname.rename()