elementtree.parse()没有打开文件open()会

时间:2013-08-15 15:24:42

标签: python file-io xml-parsing

我正在尝试编写一个python脚本,它将打开一个XML文件添加一些元素,然后退出。我无法获取元素树来打开我想要的文件。我可以用open()打开文件。示例代码

try:
    file = open(projectLocation + "\etc\config\struts-config.xml", 'r')
    print "oppenned file" #this will print
    tree = ET.parse(projectLocation + "\etc\config\struts-config.xml") #this fails
    print "oppenned xml"
except:
    print "could not open " + projectLocation + "\etc\config\struts-config.xml"
    sys.exit()

为我收到的评论更新代码:

    tree = ET.parse(projectLocation + "\etc\config\struts-config.xml")
    print "oppenned xml"

这给了我错误:

        Traceback (most recent call last):
  File "test.py", line 117, in <module>
    tree = ET.parse(projectLocation + "\etc\config\struts-config.xml")
  File "/usr/lib/python2.6/xml/etree/ElementTree.py", line 862, in parse
    tree.parse(source, parser)
  File "/usr/lib/python2.6/xml/etree/ElementTree.py", line 586, in parse
    parser.feed(data)
  File "/usr/lib/python2.6/xml/etree/ElementTree.py", line 1245, in feed
    self._parser.Parse(data, 0)
xml.parsers.expat.ExpatError: mismatched tag: line 1205, column 2

0 个答案:

没有答案