Skip to content

Instantly share code, notes, and snippets.

@TheoOliveira
Created February 10, 2024 06:41
Show Gist options
  • Save TheoOliveira/74e9fea0c1a8538d23b3bfce943e8e2e to your computer and use it in GitHub Desktop.
Save TheoOliveira/74e9fea0c1a8538d23b3bfce943e8e2e to your computer and use it in GitHub Desktop.
Remove string of characteres from folders and files
import os
import re
def remove_uuid_from_filename(folder_path):
for root, dirs, files in os.walk(folder_path):
for filename in files:
file_path = os.path.join(root, filename)
new_filename = re.sub(r"(.*)_\w{32}(\.\w+)$", r"\1\2", filename)
if new_filename != filename:
new_file_path = os.path.join(root, new_filename)
os.rename(file_path, new_file_path)
print(f"Renamed file: {filename} -> {new_filename}")
for dir_name in dirs:
dir_path = os.path.join(root, dir_name)
new_dir_name = re.sub(r"(.*)_\w{32}$", r"\1", dir_name)
if new_dir_name != dir_name:
new_dir_path = os.path.join(root, new_dir_name)
os.rename(dir_path, new_dir_path)
print(f"Renamed folder: {dir_name} -> {new_dir_name}")
# Replace 'folder_path' with the path of the folder where you want to remove UUIDs
folder_path = ""
remove_uuid_from_filename(folder_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment