Open and read sequential XML files with unknown files names in Python
Question: I wish to read incoming XML files that have no specific name (e.g. date/time
naming) to extract values and perform particular tasks. I can step through the
files but am having trouble opening them and reading them.

What I have that works is:-

    
    
     import os
     path = 'directory/'
     listing = os.listdir(path)
     for infile in listing:
          print infile
    

But when I add the following to try and read the files it errors saying No
such file or directory.

    
    
          file = open(infile,'r')
    

Thank you.


Answer: `os.listdir` provides the base names, not absolute paths. You'll need to do
`os.path.join(path, infile)` instead of just `infile` (that may still be a
relative path, which should be fine; if you needed an absolute path, you'd
feed it through `os.path.abspath`).

