ege-skill/cleaner.py

64 lines
2.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import re
def clean_all(base_dir="photo"):
# Паттерн: подчеркивание + цифры
id_pattern = re.compile(r'_\d+')
if not os.path.exists(base_dir):
print(f"[!] Папка {base_dir} не найдена.")
return
# Обходим дерево СНИЗУ ВВЕРХ
for root, dirs, files in os.walk(base_dir, topdown=False):
# --- 1. РАБОТА С ФАЙЛАМИ ---
for name in files:
old_path = os.path.join(root, name)
# Сначала чистим СОДЕРЖИМОЕ, если это отчет .md
if name.endswith('.md'):
with open(old_path, 'r', encoding='utf-8') as f:
content = f.read()
if id_pattern.search(content):
new_content = id_pattern.sub('', content)
with open(old_path, 'w', encoding='utf-8') as f:
f.write(new_content)
print(f"[CONTENT] Очищен текст в: {name}")
# Теперь ПЕРЕИМЕНОВЫВАЕМ сам файл
if id_pattern.search(name):
base, ext = os.path.splitext(name)
clean_base = id_pattern.sub('', base)
new_name = f"{clean_base}{ext}"
new_path = os.path.join(root, new_name)
# Обработка дубликатов (photo.jpg, photo_1.jpg...)
counter = 1
while os.path.exists(new_path):
new_name = f"{clean_base}_{counter}{ext}"
new_path = os.path.join(root, new_name)
counter += 1
os.rename(old_path, new_path)
print(f"[FILE] Переименован: {name} -> {new_name}")
# --- 2. РАБОТА С ПАПКАМИ ---
for name in dirs:
if id_pattern.search(name):
new_name = id_pattern.sub('', name)
old_path = os.path.join(root, name)
new_path = os.path.join(root, new_name)
if not os.path.exists(new_path):
os.rename(old_path, new_path)
print(f"[DIR] Переименована: {name} -> {new_name}")
else:
print(f"[SKIP] Папка {new_name} уже существует.")
if __name__ == "__main__":
clean_all("photo")
print("\n[OK] Полная чистка (имена + текст) завершена!")