Загрузка данных


import os
import shutil

# =====================================================================
# НАСТРОЙКИ
# =====================================================================
source_folder = "Downloads_Bank_Docs"  # Твоя главная папка
files_per_folder = 10                  # По сколько файлов класть теперь
new_prefix = "Upload_Batch_"           # Как будут называться новые папки
trash_folder = "To_Delete_Archive"     # Папка с мусором (чтобы не трогать)
# =====================================================================

def repack_batches():
    if not os.path.exists(source_folder):
        print(f"[!] Ошибка: Папка {source_folder} не найдена.")
        return

    print("--- Собираю все файлы для переупаковки... ---")

    # 1. Собираем пути ко ВСЕМ файлам во всех подпапках
    all_files = []
    for root, dirs, files in os.walk(source_folder):
        # Игнорируем папку с корзиной, чтобы не вытащить отмененные доки
        if trash_folder in root:
            continue
            
        for file in files:
            all_files.append(os.path.join(root, file))
    
    total_files = len(all_files)
    if total_files == 0:
        print("[!] Не найдено файлов для переупаковки.")
        return

    print(f"Всего файлов: {total_files}. Раскидываю по {files_per_folder} шт...")

    # 2. Создаем новые папки и переносим файлы
    folder_count = 0
    for i in range(0, total_files, files_per_folder):
        folder_count += 1
        new_folder_path = os.path.join(source_folder, f"{new_prefix}{folder_count}")
        
        if not os.path.exists(new_folder_path):
            os.makedirs(new_folder_path)
        
        batch = all_files[i : i + files_per_folder]
        for file_path in batch:
            file_name = os.path.basename(file_path)
            new_path = os.path.join(new_folder_path, file_name)
            
            # Перемещаем файл на новое место
            if file_path != new_path:
                shutil.move(file_path, new_path)

    # 3. Наводим порядок: удаляем старые пустые папки
    print("--- Удаляю пустые старые папки ---")
    for root, dirs, files in os.walk(source_folder, topdown=False):
        for d in dirs:
            dir_path = os.path.join(root, d)
            # Если папка пустая, удаляем её
            if not os.listdir(dir_path): 
                os.rmdir(dir_path)

    print(f"\n=== ГОТОВО! ===")
    print(f"Теперь всё красиво лежит в папках {new_prefix}1, 2 и т.д. ровно по {files_per_folder} файлов.")

if __name__ == "__main__":
    repack_batches()