根据txt文件复制文件到指定文件夹

根据txt文件复制文件到指定文件夹_第1张图片     根据txt文件复制文件到指定文件夹_第2张图片

需要根据txt的lable信息,将标签为0的单独提取到yes文件夹中,将标签为1的单独提取到no文件夹中。 

import os
import shutil

def mycopyfile(srcfile,dstpath):
    print(srcfile)
    if not os.path.isfile(srcfile):
        print ("%s not exist!"%(srcfile))
    else:
        fpath,fname=os.path.split(srcfile)             # 分离文件名和路径
        if not os.path.exists(dstpath):
            os.makedirs(dstpath)                       # 创建路径
        shutil.copy(srcfile, dstpath + fname)          # 复制文件
        print ("copy %s -> %s"%(srcfile, dstpath + fname))


def extract_imgs(datadir, csvfile):
    filelists = open(csvfile).readlines()
    for line in filelists[1:]:
        line = line.strip().split(',')
        name = line[1]
        label = int(line[2])
        filepath = os.path.join(datadir, name)
        if label == 0:
            # 无病样本,yes
            mycopyfile(filepath,yesdir)
        elif label == 1:
            mycopyfile(filepath,nodir)


if __name__ == '__main__':
    DATADIR2 = 'validation/PALM-Validation400'
    CSVFILE = 'valid_gt/PALM-Validation-GT/label.csv'

    yesdir = 'yes/'
    nodir = 'no/'
    if not os.path.exists(yesdir):
        os.makedirs(yesdir)
    if not os.path.exists(nodir):
        os.makedirs(nodir)

    extract_imgs(DATADIR2,CSVFILE)

根据txt文件复制文件到指定文件夹_第3张图片

 

你可能感兴趣的:(深度学习,python,python)