使用Python将大文件夹中的文件分成文件数量相等的小文件夹

使用Python将大文件夹中的文件分成文件数量相等的小文件夹:

import os
import shutil
from pathlib import Path

def split_files_into_groups(source_dir="q:/source_dir", files_per_group=1000, prefix="q:/new"):
    """
    Split files from source directory into groups and move them to numbered folders.
    
    Features:
    - Checks if source directory exists and contains files
    - Creates numbered folders (new_01, new_02, etc.)
    - Moves files in batches of specified size
    - Provides progress feedback
    - Handles errors gracefully
    
    Args:
        source_dir (str): Path to source directory (default: "sorce_dir")
        files_per_group (int): Max files per group (default: 1000)
        prefix (str): Prefix for output folders (default: "new")
    """
    try:
        # Validate source directory
        if not os.path.exists(source_dir):
            raise FileNotFoundError(f"Error: Source directory '{source_dir}' not found")
            
        if not os.path.isdir(source_dir):
            raise NotADirectoryError(f"Error: '{source_dir}' is not a directory")
            
        # Get all files (excluding subdirectories)
        all_files = [
            f for f in os.listdir(source_dir) 
            if os.path.isfile(os.path.join(source_dir, f))
        ]
        
        if not all_files:
            print(f"Info: No files found in '{source_dir}'")
            return
            
        total_files = len(all_files)
        print(f"Found {total_files} files in '{source_dir}'")
        
        # Calculate number of groups needed
        num_groups = (total_files + files_per_group - 1) // files_per_group
        print(f"Creating {num_groups} groups with max {files_per_group} files each")
        
        # Process files in batches
        for group_num in range(1, num_groups + 1):
            group_name = f"{prefix}_{group_num:02d}"  # Format as 01, 02, etc.
            os.makedirs(group_name, exist_ok=True)
            
            # Get files for current group
            start_idx = (group_num - 1) * files_per_group
            end_idx = min(group_num * files_per_group, total_files)
            group_files = all_files[start_idx:end_idx]
            
            # Move files
            print(f"\nProcessing group {group_name} ({len(group_files)} files):")
            for i, file in enumerate(group_files, 1):
                src = os.path.join(source_dir, file)
                dst = os.path.join(group_name, file)
                shutil.move(src, dst)
                if i % 100 == 0 or i == len(group_files):
                    print(f"  Moved {i}/{len(group_files)} files")
                    
        print("\nOperation completed successfully")
        
    except Exception as e:
        print(f"\nError occurred: {str(e)}")
        print("Please check:")
        print("- Source directory exists and is accessible")
        print("- You have write permissions")
        print("- There are no file naming conflicts")

if __name__ == "__main__":
    print("File Splitter Utility")
    print("=====================")
    split_files_into_groups()
    print("\nNote: Original files will be moved (not copied)")
  • 读取同级目录下的source_dir文件夹里的文件
  • 将这些文件分成每1000个文件一组
  • 创建以"new_"为前缀的编号文件夹(如new_01, new_02等)
  • 将每组1000个文件移动到对应的文件夹中
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦实学习室

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值