Skip to content

Instantly share code, notes, and snippets.

@ILPlais
Last active January 26, 2025 15:35
Show Gist options
  • Select an option

  • Save ILPlais/dbc26522a916aa9660ce7b3c11127ef1 to your computer and use it in GitHub Desktop.

Select an option

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
#!/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