Python脚本——将一个文件夹下的文件划分到不同文件夹

# 目标检测中,将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)

猜你喜欢

转载自blog.csdn.net/cunyizhang/article/details/86712961