-
-
Save WilhelmOks/535fb7c6da27d79bd8766ef0946d3ce9 to your computer and use it in GitHub Desktop.
Python script to move files randomly into folders to create random groups. Written by ChatGPT.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import random | |
import shutil | |
import math | |
import argparse | |
def move_files(directory_path, num_files_per_subdir): | |
# Get list of all files in directory | |
all_files = os.listdir(directory_path) | |
# Shuffle the list randomly | |
random.shuffle(all_files) | |
# Calculate the number of subdirectories to create | |
num_subdirs = math.ceil(len(all_files) / num_files_per_subdir) | |
# Create subdirectories | |
for i in range(num_subdirs): | |
subdir_path = os.path.join(directory_path, f"group{i+1}") | |
os.mkdir(subdir_path) | |
# Move random files to subdirectory | |
for j in range(num_files_per_subdir): | |
if all_files: | |
# Get random file and remove it from list | |
file = all_files.pop(random.randrange(len(all_files))) | |
# Move file to subdirectory | |
src_path = os.path.join(directory_path, file) | |
dst_path = os.path.join(subdir_path, file) | |
shutil.move(src_path, dst_path) | |
else: | |
break # No more files to move | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description='Randomly move files from a directory to subdirectories') | |
parser.add_argument('directory_path', type=str, help='The path to the directory containing the files to move') | |
parser.add_argument('num_files_per_subdir', type=int, help='The number of files to include in each subdirectory') | |
args = parser.parse_args() | |
move_files(args.directory_path, args.num_files_per_subdir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment