如何随机化一个文件夹中所有文件的文件名? (Python)
How to randomize the file names from all files in one folder? (Python)
我有一个包含许多文件的文件夹,我希望所有文件名都与其他文件的文件名随机化,以便文件 1 具有文件 2 的名称,但文件超过 100 个。
最好的方法是什么?
import random
import os
# Get a list of files
file_names = os.listdir()
tmp_names = [str(i) + ".tmp" for i in range(len(file_names))]
# Renaming to arbitrary filenames to avoid replacing
for old, new in zip(file_names, tmp_names):
os.replace(old, new)
# Shuffle the file names
random.shuffle(file_names)
# Randomize
for old, new in zip(tmp_names, file_names):
os.replace(old, new)
我有一个包含许多文件的文件夹,我希望所有文件名都与其他文件的文件名随机化,以便文件 1 具有文件 2 的名称,但文件超过 100 个。
最好的方法是什么?
import random
import os
# Get a list of files
file_names = os.listdir()
tmp_names = [str(i) + ".tmp" for i in range(len(file_names))]
# Renaming to arbitrary filenames to avoid replacing
for old, new in zip(file_names, tmp_names):
os.replace(old, new)
# Shuffle the file names
random.shuffle(file_names)
# Randomize
for old, new in zip(tmp_names, file_names):
os.replace(old, new)