python同时将两个文件夹更改名字

#### 将一个文件夹内的图片名称随机,对应着另外一个文件中的标签也改为相对应的名字
#coding=utf-8
import os  #打开文件时需要
from PIL import Image
import re
 
class BatchRename():
    def __init__(self):
        #我的图片文件夹路径
        self.path1 = './9-crack760/image/'
        self.path2 = './9-crack760/mask/'
 
    def rename(self):
        filelist = os.listdir(self.path1)
        total_num = len(filelist)
        print(total_num)
        i = 0 #图片编号从多少开始
        for item in filelist:
            # if item.endswith('.png'):
            # print(i,item)
            # i = i + 1
            n = 4 - len(str(i))
            src1 = os.path.join(os.path.abspath(self.path1), item)
            dst1 = os.path.join(os.path.abspath(self.path1), 'c9_' + str(0)*n + str(i) + '.png')
            src2 = os.path.join(os.path.abspath(self.path2), item)
            dst2 = os.path.join(os.path.abspath(self.path2), 'c9_' + str(0)*n + str(i) + '.png')
            try:
                os.rename(src1, dst1)
                os.rename(src2, dst2)
                print('converting %s to %s ...'  % (src1, dst1))
                i = i + 1
            except:
                continue
        print("total %d to rename & converted %d jpgs"  % (total_num, i))
if __name__ == '__main__':
    demo = BatchRename()
    demo.rename()
上一篇:Android APK反编译


下一篇:pwsh:Rename-Item: Cannot rename the specified target, because it represents a path or device name.