I can’t figure out what’s wrong. I’ve used rename before without any problems, and can’t find a solution in other similar questions.
import os
import random
directory = "C:\\whatever"
string = ""
alphabet = "abcdefghijklmnopqrstuvwxyz"
listDir = os.listdir(directory)
for item in listDir:
path = os.path.join(directory, item)
for x in random.sample(alphabet, random.randint(5,15)):
string += x
string += path[-4:] #adds file extension
os.rename(path, string)
string= ""
There are a few strange things in your code. For example, your source to the file is the full path but your destination to rename is just a filename, so files will appear in whatever the working directory is – which is probably not what you wanted.
You have no protection from two randomly generated filenames being the same, so you could destroy some of your data this way.
Try this out, which should help you identify any problems. This will only rename files, and skip subdirectories.