Last active
January 26, 2025 15:35
-
-
Save ILPlais/dbc26522a916aa9660ce7b3c11127ef1 to your computer and use it in GitHub Desktop.
Remove the numbers at the ends of files when they are between parenthesis
This file contains hidden or 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
| #!/bin/python3 | |
| import os | |
| import re | |
| def remove_numbers_from_names(directory): | |
| for root, _, files in os.walk(directory): | |
| for filename in files: | |
| # Check if filename is a directory | |
| if os.path.isfile(filename): | |
| # Extract the file name without the extension | |
| base_name, extension = os.path.splitext(filename) | |
| # Check if the file name contains numbers in parentheses | |
| if re.search(r"\s?\(\d+\)\s?$", base_name): | |
| # Remove the numbers in parentheses from the file name | |
| new_base_name = re.sub(r"\s?\(\d+\)\s?$", "", base_name) | |
| # Check if the new file name already exists | |
| new_filename = new_base_name + extension | |
| # Get the full path of the old and new file names | |
| full_filename = os.path.join(root, filename) | |
| full_new_filename = os.path.join(root, new_filename) | |
| if not os.path.exists(os.path.join(root, new_filename)): | |
| # Rename the file | |
| print(f"[*] π Rename the file \"{filename}\" to \"{new_filename}\".") | |
| try: | |
| os.rename(full_filename, full_new_filename) | |
| except Exception as e: | |
| print(f"[!] Error renaming file \"{full_filename}\":{e}.") | |
| else: | |
| # Check if the file containing numbers is bigger than the file without numbers | |
| if os.path.getsize(full_filename) > os.path.getsize(full_new_filename): | |
| # Delete the file without numbers | |
| print(f"[*] ποΈ Delete the file \"{new_filename}\".") | |
| try: | |
| os.remove(full_new_filename) | |
| except Exception as e: | |
| print(f"[!] Error deleting file \"{full_new_filename}\":{e}.") | |
| # Rename the file | |
| print(f"[*] π Rename the file \"{filename}\" to \"{new_filename}\".") | |
| try: | |
| os.rename(full_filename, full_new_filename) | |
| except Exception as e: | |
| print(f"[!] Error renaming file \"{full_filename}\":{e}.") | |
| elif os.path.getsize(full_filename) < os.path.getsize(full_new_filename): | |
| # Delete the file with numbers | |
| print(f"[*] ποΈ Delete the file \"{filename}\".") | |
| try: | |
| os.remove(full_filename) | |
| except Exception as e: | |
| print(f"[!] Error deleting file \"{full_filename}\":{e}.") | |
| else: | |
| # Keep the both files | |
| print(f"[*] π Keep the both files \"{filename}\" and \"{new_filename}\".") | |
| # Check if filename is a directory | |
| elif os.path.isdir(filename): | |
| # Check if the directory name contains numbers in parentheses | |
| if re.search(r"\s?\(\d+\)\s?$", root): | |
| # Remove the numbers in parentheses from the directory name | |
| new_root = re.sub(r"\s?\(\d+\)\s?$", "", root) | |
| # Check if the new directory name already exists | |
| if not os.path.exists(new_root): | |
| # Rename the directory | |
| print(f"[*] π Rename the directory \"{root}\" to \"{new_root}\".") | |
| try: | |
| os.rename(root, new_root) | |
| except Exception as e: | |
| print(f"\n[!] Error renaming directory \"{root}\": {e}.") | |
| if __name__ == "__main__": | |
| # Replace "target_directory" with the actual directory you want to process | |
| target_directory = os.getcwd() | |
| remove_numbers_from_names(target_directory) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment