lundi 22 juin 2015

Randomly swapping file names in Python

I need to swap the names of all the files in a folder but each file has to have it's own unique name.

I tried to loop through the folder adding all the files to a list and then shuffling that list with random.shuffle(), and then looping through the folder again but this time renaming each file by order to the shuffled list.

It was something like this:

for file in os.listdir("images/"):
    os.rename(file, files_shuffle[i])
    i += 1

But I get WinError 183 "Cannot create a file when that file already exists". What would be the best way to approach this problem?




Aucun commentaire:

Enregistrer un commentaire