You can download this code by clicking the button below.
This code is now available for download.
This function first copies all files from the source directory to the destination directory, then lists all files in the destination directory, shuffles them randomly. After that, the function renames and moves each file to the destination directory to achieve random sorting of files.
Technology Stack : shutil, os, random
Code Type : The type of code
Code Difficulty : Intermediate
import shutil
import random
def shuffle_files(src, dst):
"""
Shuffle the files in the source directory and move them to the destination directory.
"""
shutil.copytree(src, dst)
files = [f for f in os.listdir(dst) if os.path.isfile(os.path.join(dst, f))]
random.shuffle(files)
for file in files:
shutil.move(os.path.join(dst, file), os.path.join(dst, f"{random.randint(0, 10000)}.tmp"))
os.rename(os.path.join(dst, f"{random.randint(0, 10000)}.tmp"), os.path.join(dst, file))