Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import random
- def randomize_file_names():
- # Get the current working directory
- directory = os.getcwd()
- # Dictionary to store files by their extensions
- file_types = {}
- # List of file extensions to exclude from renaming
- excluded_extensions = {'.py', '.aep', '.psd', '.prproj'}
- # Step 1: Collect all files in the directory, sorted by file type
- for filename in os.listdir(directory):
- file_path = os.path.join(directory, filename)
- if os.path.isfile(file_path):
- file_ext = os.path.splitext(filename)[1].lower() # Get file extension
- if file_ext in excluded_extensions:
- continue # Skip excluded file types
- if file_ext not in file_types:
- file_types[file_ext] = []
- file_types[file_ext].append(filename)
- # Step 2: Randomize file names within each file type
- for file_ext, files in file_types.items():
- if len(files) < 2:
- continue # Skip types with less than two files, no need to randomize
- # Generate a randomized list of filenames for renaming
- random_names = random.sample(files, len(files))
- # Temporary renaming to avoid conflicts
- temp_names = [f"temp_{i}{file_ext}" for i in range(len(files))]
- for original, temp in zip(files, temp_names):
- os.rename(os.path.join(directory, original), os.path.join(directory, temp))
- # Final renaming to randomized names
- for temp, new_name in zip(temp_names, random_names):
- os.rename(os.path.join(directory, temp), os.path.join(directory, new_name))
- print(f"Randomized {file_ext} files:", random_names)
- # Run the function in the current working directory
- randomize_file_names()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement