首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有lxml etree解析函数的IOError

具有lxml etree解析函数的IOError
EN

Stack Overflow用户
提问于 2011-06-14 17:37:08
回答 1查看 3.1K关注 0票数 1

我的逻辑是这样的:

代码语言:javascript
复制
for root, dirs, files in os.walk(os.getcwd()):
    if "info.xml" in files:
        root = lxml.etree.parse("%s/info.xml" % root)
        tag = root.xpath("/info/tagname")[0].text

当解析当前路径中非常深的一个info.xml时,遇到错误消息:

代码语言:javascript
复制
    Traceback (most recent call last):
  File "/home/work/mergefile.py", line 365, in <module>
  File "/home/work/mergefile.py", line 344, in merge_ejb_files
  File "/home/work/mergefile.py", line 63, in __init__
  File "/home/work/mergefile.py", line 78, in _parse_info2doc
  File "lxml.etree.pyx", line 2698, in lxml.etree.parse (src/lxml/lxml.etree.c:49590)
  File "parser.pxi", line 1491, in lxml.etree._parseDocument (src/lxml/lxml.etree.c:71205)
  File "parser.pxi", line 1520, in lxml.etree._parseDocumentFromURL (src/lxml/lxml.etree.c:71488)
  File "parser.pxi", line 1420, in lxml.etree._parseDocFromFile (src/lxml/lxml.etree.c:70583)
  File "parser.pxi", line 975, in lxml.etree._BaseParser._parseDocFromFile (src/lxml/lxml.etree.c:67736)
  File "parser.pxi", line 539, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:63820)
  File "parser.pxi", line 625, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:64741)
  File "parser.pxi", line 563, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:64056)
IOError: Error reading file '/home/work/ci/case/dc_daily/dc/213577/223922/223958/792536/info.xml': failed to load external entity "/home/work/ci/case/dc_daily/dc/213577/223922/223958/792536/info.xml"

但是文件"/home/work/ci/case/dc_daily/dc/213577/223922/223958/792536/info.xml"是存在的,我可以在ipython IDE下用lxml解析它

你知道问题出在哪里吗?如果你知道,请帮帮我!谢谢!

EN

回答 1

Stack Overflow用户

发布于 2011-08-06 12:28:51

根据我上面的评论,这是我的解决方案。我正在打开文件以供读取,他们在打开后立即将其关闭,这样我就不会达到1024个文件的限制。

代码语言:javascript
复制
import lxml.etree as etree
for root,dirs,files in os.walk(os.getcwd()):
    if "info.xml" in files:
        with open('%s/info.xml'%root) as processfile: #use 'rb' if necessary
            xml = etree.parse(processfile)
            tag = root.xpath("/info/tagname")[0].text
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6341659

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档