import os import shutil src_dir = "tome" dst_dir = "tome_fix" os.makedirs(dst_dir, exist_ok=True) for root, _, files in os.walk(src_dir): for f in files: rel_path = os.path.relpath(root, src_dir) # Replace path separators with underscores path_part = rel_path.replace(os.sep, "_") # If src_dir is the root with no subfolders, path_part will be "." if path_part == ".": new_name = f else: new_name = f"{path_part}_{f}" src_path = os.path.join(root, f) dst_path = os.path.join(dst_dir, new_name) # Ensure uniqueness (just in case) counter = 1 base, ext = os.path.splitext(new_name) while os.path.exists(dst_path): dst_path = os.path.join(dst_dir, f"{base}_{counter}{ext}") counter += 1 shutil.move(src_path, dst_path)