手头有一个几万张图片需要进行打乱顺序,并重新命名,使用python来实现
第一步先对文件夹内的文件批量打乱顺序
第二步重新排序
第三步批量重命名
import os ,random , time def rename(): filetype = '.png' filename = '' zfillNum = 5 path = r"C:\Users\xxxx\Desktop\test" i = 0 filelist = os.listdir(path)
random.shuffle(filelist) for files in filelist: Olddir = os.path.join(path, files) if os.path.isdir(Olddir): continue filenameRes = str(int(time.time())+random.randint(1000000,88888888)) Newdir = os.path.join(path, filenameRes + filetype) os.rename(Olddir, Newdir)
filelist = os.listdir(path) for files in filelist: i = i + 1 Olddir = os.path.join(path, files) if os.path.isdir(Olddir): continue filenameNum = str(i).zfill(zfillNum) filenameRes = filename+filenameNum Newdir = os.path.join(path, filenameRes + filetype) os.rename(Olddir, Newdir) return True
if __name__ == '__main__': rename()
|