根据图片文件夹中图片名找txt文件夹中对应txt文件,找到后存储于新文件夹中

import os
import shutil

def isexist(name, path=None):
    '''

    :param name: 需要检测的文件或文件夹名
    :param path: 需要检测的文件或文件夹所在的路径,当path=None时默认使用当前路径检测
    :return: True/False 当检测的文件或文件夹所在的路径下有目标文件或文件夹时返回Ture,
            当检测的文件或文件夹所在的路径下没有有目标文件或文件夹时返回Falsefin
    '''
    if path is None:
        path = os.getcwd()
    if os.path.exists(path + '/' + name):
        print("Under the path: " + path + '\n' + name + " is exist")
        return True
    else:
        if (os.path.exists(path)):
            print("Under the path: " + path + '\n' + name + " is not exist")
        else:
            print("This path could not be found: " + path + '\n')
        return False

img_root = "图片文件夹路径"
count = 0
txtsroot = "txt文件夹路径"
save_folder = "即将要保存的文件夹"
if not os.path.exists(save_folder):
    os.mkdir(save_folder)
fileList = os.listdir(img_root)
for img_path in fileList:
    picsname = os.path.basename(img_path)
    txtname = picsname.replace(".jpg", ".txt")
    txt_path = os.path.join(txtsroot, txtname)
    if isexist(txtname, txtsroot):
        count += 1
        new_file_path = os.path.join(save_folder, txtname)
        shutil.move(txt_path, new_file_path)
print("finish!", count)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一鹿向晗99

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值