将一个包含15.4w个文件的文件夹,分割为155个文件夹,其中每个文件夹包含1000个文件

# -- coding: utf-8 --
import os
import shutil


# 分割前文件夹路径
source_directory = 'D:\Download\Annotations'

# 分割后文件夹路径
destination_directory = 'D:\Download\Annotations_split'

# 创建新文件夹
for i in range(1, 156):  # 新建155个文件夹
    subfolder_path = os.path.join(destination_directory, f'Annotations_{i}')
    os.makedirs(subfolder_path, exist_ok=True)

# 读取原文件名称列表
file_list = os.listdir(source_directory)

# 对原文件名称列表排序
sorted_files = sorted(file_list)

# 按照每1000个文件一组进行分组,若剩下不足1000则为最后一组
file_groups = [sorted_files[i:i+1000] for i in range(0, len(sorted_files), 1000)]

# 使用copy生成新的文件夹
for i, group in enumerate(file_groups):
    for filename in group:
        source_path = os.path.join(source_directory, filename)
        destination_path = os.path.join(destination_directory, f'Annotations_{i+1}', filename)
        shutil.copy(source_path, destination_path)

print("Files have been ranked and split into subfolders.")

  • 应用场景:使用Colab的时候不能操作包含大量文件的文件夹,无论是上传还是读取都会出现文件丢失或速度很慢的情况,所以可以使用该方法分割为较少的文件夹。
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐