Need to randomize order of tens of thousands of images then rename sequentially.

Steps:

  1. Shuffle directory file list.
  2. Rename temporarily with random timestamps to break original ordering.
  3. List again and assign zero-padded incremental names.
import os, random, time
def rename():
filetype = '.png' # target extension
filename = '' # prefix
zfillNum = 5 # digits
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()