Created
September 12, 2024 07:40
-
-
Save vadviktor/1d8ad0dc3bb5b7117492fe9b4bc9e3c0 to your computer and use it in GitHub Desktop.
Sanitize file names in a directory tree
This file contains 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
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