0

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?

glls
  • 2,325
  • 1
  • 22
  • 39
Vrankela
  • 1,162
  • 3
  • 16
  • 39
  • 1
    Don't use `is` to compare strings. It should work if you use `if objname=='stop':`. – Aran-Fey May 27 '16 at 07:52
  • damn it, I purposefully used is because == reported an error for some reason when I first tried it. But now it works... Thank you! If you want you can write an answer @Rawing so i can give you award you as answered.... – Vrankela May 27 '16 at 07:58
  • can you print objname and check what it contains to make sure at some point it has. If you have a space in object name then you can try if 'stop' in objename: – Vikas Madhusudana May 27 '16 at 07:59

1 Answers1

2

Don't use is to compare strings, use ==:

if objname=='stop':

The differences between is and == have been discussed in detail in this thread.

Community
  • 1
  • 1
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149