赞
踩
# -- 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.")
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。