按照图片名称拷贝标签文件(python)

代码主要被用来按照图片名称拷贝标签文件

# -*- coding: utf-8 -*-
"""
Created on Mon Aug 31 21:23:07 2020

@author: 奥利给
@author: deyiwang@qq.com
"""

import datetime
import io, os
import shutil




def record_name(inshore):
    # 首先遍历inshore文件夹, 将其中所有文件名导出制作成inshore_name的list
    inshore_name = []
    for roots, dirs, files in os.walk(inshore):
        for index in range(len(files)):
            formatFile = files[index].split('.')[-1]
            if formatFile == 'jpg':
                inshore_name.append(files[index].split('.')[0])
                
    print(f'the length of list:{len(inshore_name)}')
    return inshore_name

# 导入inshore_list, 遍历annotation文件夹, 寻找与inshore_list一致的文件
def search_name_copy(inshore_list, annotation_path):
    count = 0
    file_list = []
    for roots, dirs, files in os.walk(annotation_path):
        for index in range(len(files)):
            formatName = files[index].split('.')[0]
            if formatName in inshore_list:
                count += 1
#                print(os.path.join(annotation_path, files[index]))
                file_list.append(os.path.join(annotation_path, files[index]))
    print(count)
    return file_list

if __name__ == '__main__':
    
    inshore = 'F:\\xxxx\\xxxx'
    annotation = 'F:\\xxxx\\xxxx'
    out_annotation = 'F:\\xxxx\\xxxx'       
    if not os.path.exists(out_annotation):
        os.mkdir(out_annotation)  # 创建目录
    
#    inshore_list = record_name(inshore)
    
    carry_list = search_name_copy(record_name(inshore), annotation)
    
    for files in carry_list:
    #    print(os.path.join(out_annotation,files.split('\\')[-1]))
        oldFilePath = files
        newFilePath = os.path.join(out_annotation,files.split('\\')[-1])
        shutil.copyfile(oldFilePath, newFilePath)
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

deyiwang89

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

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

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

打赏作者

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

抵扣说明:

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

余额充值