【发布时间】:2020-12-25 23:06:54
【问题描述】:
我在函数定义中有一段代码:
try:
with open(requests,'rt') as f:
tree = ElementTree.parse(f)
字符串 requests 包含一个文件路径,显然该文件已打开。 在.py文件的开头,我有
from xml.etree.ElementTree import ElementTree
当我在 test.py 中尝试这些行并调用“python3 test.py”时,我没有收到错误消息,但是当我使用 python3 运行程序时,我收到以下错误消息:
tree = ElementTree.parse(f)
TypeError: parse() missing 1 required positional argument: 'source'
然而,你可以看到 parse() 的位置参数是 f。我确实放了一个打印命令来检查请求的值,它显示了正确的文件名。
【问题讨论】:
标签: python python-3.x typeerror elementtree open-with