Python:查看txt内容指向的文件是否存在

这个程序用于检测存在txt中的文件名称列表里的文件,是否真的存在,输入两个路径:

1.txt文档的路径

2.图像或其他文件所在路径

如果不是图像,则自己修改一下,是的话直接复制,修改路径就行。

# -*- coding: utf-8 -*-
import os
"""
https://blog.csdn.net/gusui7202/article/details/83239142
qhy。
"""
fi=open('H:/qhy_database/Dataset_007/VOC2007/ImageSets/Main/test.txt')
txt=fi.readlines()
XMLS=[]
count=0
for line in txt:
    count+=1
    line=line.strip('\n')
    line=(line+'.jpg')
    XMLS.append(line)

print("xml标签的数量为:{}".format(count))
fi.close()
ImagePath=('H:/qhy_database/Dataset_007/VOC2007/JPEGImages')
Imagelist=os.listdir(ImagePath)
for T in Imagelist:
   if 'JPG' in T:
      print(T)
acount=0
for xml in XMLS:
    if xml in Imagelist:
       continue
    else:
       acount+=1
       print("标签:{}在图像文件夹中找不到。".format(xml))
print("检索完毕,请对{}个没有找到对应图片的标签进行处理。".format(acount))
#运行之后发现问题,原来的标签可能保存的全是字符串??因为复制搜索的时候,除了一张图片,其他的都能找到。可能matlab读取标签并随机分配的时候,存在test里面的是字符串。
#下面检查一下变量
print(type(XMLS[0]))
print(type(Imagelist[0]))
#发现类型一样的,都是字符串。
#问题出在我写demo.py的时候,没有去除换行符。

猜你喜欢

转载自blog.csdn.net/gusui7202/article/details/83002677