File size: 879 Bytes
19f5d6e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
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)
|