Python脚本——将一个文件夹下的文件划分到不同文件夹
程序员文章站
2022-03-03 10:17:53
...
# 目标检测中,将pascal格式的数据标签Annotations按照train.txt、val.txt分成对应的文件夹
# 用在retinanet中的训练集和验证集
import os
import shutil
def split_file(src_file_path, dst_file, dst_file_path):
file = open(dst_file, 'r').readlines()
for line in file:
line = line.strip('\n')
src_file_name = src_file_path + '{}.xml'.format(line)
if not os.path.isfile(src_file_name):
print("%s not exist !!!"%(src_file_name))
else:
dst_file_name = dst_file_path + '{}.xml'.format(line)
shutil.copyfile(src_file_name,dst_file_name)
print("copy %s -->> %s"%(src_file_name, dst_file_name))
if __name__ == '__main__':
src_path = '/data/Annotations/'
train_path = '/data/train_set/'
val_path = '/data/val_set/'
train_file = '/data/ImageSets/Main/train.txt'
val_file = '/data/ImageSets/Main/val.txt'
split_file(src_path, train_file, train_path)
split_file(src_path, val_file, val_path)
上一篇: 前置守卫-包含白名单
推荐阅读
-
Python实现合并同一个文件夹下所有txt文件的方法示例
-
对python同一个文件夹里面不同.py文件的交叉引用方法详解
-
Python合并同一个文件夹下所有PDF文件的方法
-
用Python编写一个每天都在系统下新建一个文件夹的脚本
-
python移动文件:将一个文件夹中的特定后缀名的图片移动到另一个文件夹
-
PYTHON实现将一个文件夹下的名字满足条件的图片拷贝复制到另一个文件夹路径
-
python引入不同文件夹下的自定义模块方法
-
如何利用Python中的OS库将文件夹下面的文件按照自己设定的名称以及序号重命名
-
Python实现合并同一个文件夹下所有txt文件的方法示例
-
用BAT脚本实现虚拟python环境下的jupyter notebook在特定文件夹下启动