I am iterating over an entire directory with more than 5k .xml files and I want the file name only if it contains the string 'stop' under the tag <name>
which itself is a child of the tag <object>
:
import xml.etree.ElementTree as etree
import os
path = '/home/devf12/Documents'
listofnames = []
for filename in os.listdir(path):
if not filename.endswith('.xml'): continue
fullname = os.path.join(path, filename)
tree = etree.parse(fullname)
root = tree.getroot()
objects = [tag for tag in root.findall('object')]
for tag in objects:
objname = tag.findtext('name')
print filename # this is the last place that it will print the filename
if objname is 'stop': # but I need to narrow it down if it meets this condition
print filename # why wont it print here?
Does anyone know why my current code doesn't achieve this and how it can be done?