将一个包含15.4w个文件的文件夹,分割为155个文件夹,其中每个文件夹包含1000个文件
import os
import shutil
source_directory = 'D:\Download\Annotations'
destination_directory = 'D:\Download\Annotations_split'
for i in range(1, 156):
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)
file_groups = [sorted_files[i:i+1000] for i in range(0, len(sorted_files), 1000)]
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的时候不能操作包含大量文件的文件夹,无论是上传还是读取都会出现文件丢失或速度很慢的情况,所以可以使用该方法分割为较少的文件夹。