music-library-tools/misc/fix_utf8_filenames_only.py
2025-12-20 12:02:36 +02:00

27 lines
972 B
Python

import os
import re
ROOT = "/home/fucksophie/media/Music/axxturel loosies"
SUPPORTED_EXTS = {".mp3", ".m4a"} # add other formats as needed
def sanitize_filename(filename: str) -> str:
# Keep ASCII printable characters; replace anything else with "_"
return re.sub(r"[^\x20-\x7E]", "_", filename)
def safe_print(s: str):
# Encode with backslashreplace to avoid crashing on surrogates
print(s.encode("utf-8", errors="backslashreplace").decode("utf-8"))
for root, _, files in os.walk(ROOT):
for f in files:
ext = os.path.splitext(f)[1].lower()
if ext not in SUPPORTED_EXTS:
continue
old_path = os.path.join(root, f)
new_name = sanitize_filename(f)
new_path = os.path.join(root, new_name)
if old_path != new_path:
safe_print(f"[DRY RUN] Would rename: {old_path}{new_path}")
# To apply changes, uncomment the next line:
os.rename(old_path, new_path)