Skip to content

Instantly share code, notes, and snippets.

@vadviktor
Created September 12, 2024 07:40
Show Gist options
  • Save vadviktor/1d8ad0dc3bb5b7117492fe9b4bc9e3c0 to your computer and use it in GitHub Desktop.
Save vadviktor/1d8ad0dc3bb5b7117492fe9b4bc9e3c0 to your computer and use it in GitHub Desktop.
Sanitize file names in a directory tree
import argparse
import os
import re
def sanitize_filename(filename):
# Replace characters not allowed in Windows filenames with underscores
sanitized = re.sub(r'[<>:"/\\|?*]', "_", filename)
# Remove non-ASCII characters
sanitized = re.sub(r"[^\x00-\x7F]", "_", sanitized)
return sanitized
def rename_files_in_directory(directory):
for root, dirs, files in os.walk(directory):
for filename in files:
sanitized_name = sanitize_filename(filename)
if sanitized_name != filename:
old_path = os.path.join(root, filename)
new_path = os.path.join(root, sanitized_name)
os.rename(old_path, new_path)
print(f"Renamed: {old_path} -> {new_path}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Rename files in a directory to replace invalid characters with underscores."
)
parser.add_argument("directory", type=str, help="The directory to process")
args = parser.parse_args()
rename_files_in_directory(args.directory)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment