Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import re
- def clean_filenames(directory):
- # Regex pattern to match any text in parentheses and any spaces before it
- pattern = r'\s*\(.*?\)'
- # File extensions to process
- extensions = (".iso", ".cue", ".bin")
- # Loop through all files in the directory
- for filename in os.listdir(directory):
- if filename.endswith(extensions):
- # Create the new filename by removing the pattern
- new_filename = re.sub(pattern, '', filename)
- # Construct full file paths
- old_file = os.path.join(directory, filename)
- new_file = os.path.join(directory, new_filename)
- # Rename the file
- os.rename(old_file, new_file)
- print(f'Renamed: {filename} -> {new_filename}')
- # Usage example
- directory = 'C:\\Users\\irons\\Desktop\\iso' # Replace with your directory path
- clean_filenames(directory)
Advertisement
Add Comment
Please, Sign In to add comment