ironsniper01

clean_iso_files

Sep 21st, 2024
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. import os
  2. import re
  3.  
  4. def clean_filenames(directory):
  5. # Regex pattern to match any text in parentheses and any spaces before it
  6. pattern = r'\s*\(.*?\)'
  7.  
  8. # File extensions to process
  9. extensions = (".iso", ".cue", ".bin")
  10.  
  11. # Loop through all files in the directory
  12. for filename in os.listdir(directory):
  13. if filename.endswith(extensions):
  14. # Create the new filename by removing the pattern
  15. new_filename = re.sub(pattern, '', filename)
  16.  
  17. # Construct full file paths
  18. old_file = os.path.join(directory, filename)
  19. new_file = os.path.join(directory, new_filename)
  20.  
  21. # Rename the file
  22. os.rename(old_file, new_file)
  23. print(f'Renamed: {filename} -> {new_filename}')
  24.  
  25. # Usage example
  26. directory = 'C:\\Users\\irons\\Desktop\\iso' # Replace with your directory path
  27. clean_filenames(directory)
Advertisement
Add Comment
Please, Sign In to add comment