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

     

需要根据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)

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值