1

I am currently learning python to automate a few things in my job. I need to retrieve a tag value from multiple xml files in a directory. The directory has many subfolders too.

I tried the following code, and understood what is missing. But I am not able to fix this. Here is my code:

from xml.dom.minidom import parse, parseString
import os
def jarv(dir):
for r,d,f in os.walk(dir):
    for files in f:
        if files.endswith(".xml"):
            print files
                dom=parse(files)
            name = dom.getElementsByTagName('rev')
            print name[0].firstChild.nodeValue
jarv("/path)

I understand that while executing the dom=parse(files) line, it has got the filename without the path. So it says no such files/directory.

I don't know how to fix this.

2
  • if you learn python, don't hesitate to use print. It's really useful to debug. Before the line dom=parse(files) add the line print files and you will see what it contains Commented May 6, 2013 at 10:57
  • Yes, thanks. It is really useful. It prints all the xml files in the subfolders. But i am stuck in giving the path to the parse keyword. I didnt know the syntax. I am just 1 day old in python. Commented May 6, 2013 at 11:09

1 Answer 1

1

You have to use os.path.join() to build the correct path from the dirname and the filename:

dom=parse(os.path.join(r, files))

should do it

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks bro. I changed the line as "dom=parse(os.path.join(dir, files))". Now it is working for the files in the first folder, and throwing error again.
for what files does it throws error ? have you checked the complete path that are printed, do the files really not exists ? which error is thrown now?
I have 1 root folder and 5 subfolders. i have 6 files( 1 in root, and 1 in all subfolders). When i run the command, it gives the correct output for the file in the root, and throwing error for the next file. i.e. file in the first subfolder. The error is the same as before. error no 2, no such file/directory
If you're in a subfolder you have to take that into the path as well. What you are refering to as r, d and f is actually the dirpath, dirname and the filenames. you can still use os.path.join() to build the correct path for every subfolder no mather how deep you are inside the tree. For a little learning experience print the 3 variables inside the forloop and see how they change. I have also updated my question. Now the code should work for all your files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.