I have a piece of code in a function definition which is:

    try:
        with open(requests,'rt') as f:
            tree = ElementTree.parse(f)

The string, requests, contains a file path and apparently that file is opened. At the beginning of the .py file, I have

from xml.etree.ElementTree import ElementTree

When I try these lines in test.py and call "python3 test.py" I do NOT get an error message, however when I run the program with python3 I get the following error message:

    tree = ElementTree.parse(f)
TypeError: parse() missing 1 required positional argument: 'source'

However as you can see the positional argument of parse() is f. I did put a print command to examine the value of requests, and it showed the proper file name.

1

There are 1 best solutions below

0
On

You should invoke the parse method on an ElementTree instance:

e.g.

from xml.etree.ElementTree import ElementTree
tree = ElementTree()
tree.parse("index.xhtml")

Code fix:

try:
        with open(requests,'rt') as f:
            tree = ElementTree()
            tree.parse(f)