当前位置: 首页> 汽车> 时评 > 指定文件夹按指定数量分组文件

指定文件夹按指定数量分组文件

时间:2025/7/10 0:40:12来源:https://blog.csdn.net/Jack_software/article/details/139472809 浏览次数: 0次

指定文件夹按指定数量分组文件

背景和环境说明

本程序使用 python 3.10+
机器学习,打标中需要对数据集进行分批次,比如10个人,需要分10组,熟练的人分多个点等情况。需要对标注的图片进行分组,分批次导入标注系统进行标注。

依赖包下载

pip install -U NStudyPy

使用

from NStudyPy import PyFileif __name__ == '__main__':   PyFile.random_split_s(source_dir=r'F:\temp\cards', target_dir=r'F:\temp\target', s=(100, 400, 250, 250))

分组后 在 F:\temp\target 文件夹下面 有4个文件夹,分别对应 100, 400, 250, 250 分组。文件数目太少会提前退出。

核心源码

def random_split_s(source_dir: str, target_dir: str, s=(100, 400, 250, 250)) -> None:"""随机拆分文件; 不处理子文件夹:param source_dir: 源文件夹:param target_dir: 目标文件夹:param s: 拆分个数 (100, 400, 250, 250) 是每个分组的个数,直至所有文件都拆分完毕:return: None"""files = os.listdir(source_dir)random.shuffle(files)sum_s = 0for idx, count in enumerate(s):_no = f'{idx:03}'image_g_dir = os.path.join(target_dir, _no)if not os.path.exists(image_g_dir):os.makedirs(image_g_dir)for file_name in files[sum_s:sum_s + count]:shutil.copy(os.path.join(source_dir, file_name), os.path.join(image_g_dir, file_name))sum_s += countif sum_s >= len(files):break# Done it
关键字:指定文件夹按指定数量分组文件

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: