python,根据文件名查找图片并复制

最近需要根据txt中的文件名,找到对应的图片,并放到对应文件夹。(和划分数据集有关)写了下面这段代码实现。

import os
import shutil

list_path='/home/zhouyf/insky_work/pythonProject/ImageSets/Main'
src_img_path='/home/zhouyf/insky_work/pythonProject/images'
to_img_path='/home/zhouyf/insky_work/pythonProject/ImageSets/image'

for type in os.listdir(list_path):
    names = []
    print(type)
    with open(list_path+'/'+type, 'r') as file_to_read:
        while True:
            name = file_to_read.readline()
            if not name:
                break
            name = name.strip('\n')
            names.append(name)
    #print(names)

    for name in names:
        for file in os.listdir(src_img_path):
            if os.path.isfile(src_img_path+'/'+file):
                if name in file:
                    if not os.path.exists(to_img_path + '/' + type[0:-4]):
                        os.mkdir(to_img_path + '/' + type[0:-4])
                    shutil.copy(src_img_path + '/' + file, to_img_path + '/' + type[0:-4]+ '/'+file)
                    print(name+'.jpg')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值