作者mizumutsuki (真夜)
看板Python
标题[问题] 在读取xml档时碰到瓶颈
时间Sun Sep 23 20:51:33 2018
我正在看精通python那本书学习,在第八章学习读取XML档时碰到困难
是在该书第198、199页。
这章节有个xml的范例是menu.xml,要用ElementTree来解析这个档案。
但执行时碰到问题。
按步骤,前两行是这样:
>>> import xml.etree.ElementTree as et
>>> tree=et.ElementTree(file='menu.xml')
但是我只要执行第二行,就会跑出这个错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File
"C:\Users\mizum\AppData\Local\Programs\Python\Python36-32\lib\xml\etree\ElementTree.py",
line 557, in __init__
self.parse(file)
File
"C:\Users\mizum\AppData\Local\Programs\Python\Python36-32\lib\xml\etree\ElementTree.py",
line 586, in parse
source = open(source, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'menu.xml'
看起来找不到要读取的menu.xml档
请问我要把那个xml档放在哪个资料夹,才能被python读取到呢
小弟新手,之前完全没有学过程式语言,麻烦各位大大解答疑惑了,谢谢
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 1.172.172.168
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1537707095.A.180.html
1F:→ stucode: 放在当前工作资料夹,不确定在哪的话跑一下: 09/23 21:13
2F:→ stucode: import os; print(os.getcwd()) 09/23 21:13
3F:推 rexyeah: file='C:\your_path\menu.html' 给绝对路径 09/23 21:14
4F:→ mizumutsuki: 谢谢以上大大,我马上来试 09/23 21:34
5F:→ mizumutsuki: 没问题了,谢谢大家 09/23 21:36