0

I am writing a piece of code that extracts data from a bunch of XML documents.

The code works as intended on the files individually; however, when I iterate over the files I get a wierd error.

The code goes as follows:

import xml.etree.ElementTree as ET
import os

for root,dirs,files in os.walk(path):
    for file in files:
        if file.endswith(".xml"):
            tree = ET.parse(os.path.join(root,file))
            root = tree.getroot()

When I execute the code, the following error appears:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-85cdfa81e486> in <module>()
      4     for file in files:
      5         if file.endswith(".xml"):
----> 6             tree = ET.parse(os.path.join(root,file))
      7             root = tree.getroot()

~/.pyenv/versions/3.6.0/lib/python3.6/posixpath.py in join(a, *p)
     76     will be discarded.  An empty last part will result in a path that
     77     ends with a separator."""
---> 78     a = os.fspath(a)
     79     sep = _get_sep(a)
     80     path = a

TypeError: expected str, bytes or os.PathLike object, not xml.etree.ElementTree.Element 

If I remove de last line root = tree.getroot() then everything starts working again. I don't have the slightest idea of what's happening.

1 Answer 1

2

You're using same name (root) for 2 different variables in your code (for looping through your path, and another for getting xml's root):

tree = ET.parse(os.path.join(root,file)) #root for your path/folder structure
root = tree.getroot() #root for your xml tree - should use different name

Use different variable name for one of them.

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

Comments

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.