Advertisement
Guest User

Untitled

a guest
Nov 1st, 2024
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. import os
  2. import random
  3.  
  4. def randomize_file_names():
  5. # Get the current working directory
  6. directory = os.getcwd()
  7.  
  8. # Dictionary to store files by their extensions
  9. file_types = {}
  10.  
  11. # List of file extensions to exclude from renaming
  12. excluded_extensions = {'.py', '.aep', '.psd', '.prproj'}
  13.  
  14. # Step 1: Collect all files in the directory, sorted by file type
  15. for filename in os.listdir(directory):
  16. file_path = os.path.join(directory, filename)
  17. if os.path.isfile(file_path):
  18. file_ext = os.path.splitext(filename)[1].lower() # Get file extension
  19. if file_ext in excluded_extensions:
  20. continue # Skip excluded file types
  21. if file_ext not in file_types:
  22. file_types[file_ext] = []
  23. file_types[file_ext].append(filename)
  24.  
  25. # Step 2: Randomize file names within each file type
  26. for file_ext, files in file_types.items():
  27. if len(files) < 2:
  28. continue # Skip types with less than two files, no need to randomize
  29.  
  30. # Generate a randomized list of filenames for renaming
  31. random_names = random.sample(files, len(files))
  32.  
  33. # Temporary renaming to avoid conflicts
  34. temp_names = [f"temp_{i}{file_ext}" for i in range(len(files))]
  35. for original, temp in zip(files, temp_names):
  36. os.rename(os.path.join(directory, original), os.path.join(directory, temp))
  37.  
  38. # Final renaming to randomized names
  39. for temp, new_name in zip(temp_names, random_names):
  40. os.rename(os.path.join(directory, temp), os.path.join(directory, new_name))
  41.  
  42. print(f"Randomized {file_ext} files:", random_names)
  43.  
  44. # Run the function in the current working directory
  45. randomize_file_names()
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement