挑选文件夹下图片的对应xml文件,Python

#coding=utf-8
import os
#import os.path
import shutil  #Python文件复制相应模块

label_dir=r'/home/.../Annotations'  #所有xml文件所在文件夹
annotion_dir='/home/.../picturexml'  #粘贴对应图片名称的xml文件到指定文件夹
path = '/home/.../picture'   #图片文件夹
path_list = os.listdir(path)# os.listdir(file)会历遍文件夹内的文件并返回一个列表
#print(path_list)
path_name=[]  # 定义一个空列表,不需要path_list中的后缀名
# 利用循环历遍path_list列表并且利用split去掉后缀名
for i in path_list:
    path_name.append(i.split(".")[0])
#print(path_name)
# 排序一下
path_name.sort()
for file_name in path_name:
    # "a"表示以不覆盖的形式写入到文件中,当前文件夹如果没有"save.txt"会自动创建
    with open("save.txt","a") as f:
        f.write(file_name + "\n")
        #print(file_name)
    f.close()
f = open("save.txt","r")   #设置文件对象
lines= f.readlines() 
#print (lines)
s=[]
for line in lines:
    line = line.strip()
    print (line)  
    tempxmlname='%s.xml'%line
    print(tempxmlname)
    xmlname=os.path.join(label_dir,tempxmlname)
    print (xmlname)
    os.listdir(label_dir)
    shutil.copy(xmlname,annotion_dir)
    

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值