Need to randomize order of tens of thousands of images then rename sequentially.
Steps:
- Shuffle directory file list.
- Rename temporarily with random timestamps to break original ordering.
- List again and assign zero-padded incremental names.
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 += 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()
|