我在函数定义中有一段代码是:
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。我确实放了一个打印命令来检查请求的值,它显示了正确的文件名。