I have a folder with more than 1.000 files that's updated constantly. I'm using a script to add a random number to it based on the total of the files, like this:
Before
file_a
file_b
After
1_file_a
2_file_b
I would like to add leading zeros so that the files are sorted correctly. Like this:
0001_file_a
0010_file_b
0100_file_c
Here's the random number script:
import os
import random
used_random = []
os.chdir('c:/test')
for filename in os.listdir():
n = random.randint(1, len(os.listdir()))
while n in used_random:
n = random.randint(1, len(os.listdir()))
used_random.append(n)
os.rename(filename, f"{n}_{filename}")