【问题标题】:python os.path.walk can't open filepython os.path.walk 无法打开文件
【发布时间】:2013-02-01 06:44:12
【问题描述】:
import os
from os.path import splitext,basename
import sys
from lxml import etree

#clean out old files
def cleanBrds(args, dirname,files):
    print "checking "+dirname
    for file in files:
        if file.endswith("_pyclean.brd"):
            if os.path.exists(file):
                print "removing "+file
                os.remove(file)

#prep files
def sanBrds(args, dirname,files):
    print "checking "+dirname
    for file in files:
        if file.endswith(".brd"):
            print "found "+file
            newfile = splitext(basename(file))[0]+"_pyclean.brd"
            tree=etree.parse(file)
            for elem in tree.xpath('//signal'):
                elem.getparent().remove(elem)
            f=open(newfile,'w')
            f.write(etree.tostring(tree))
            f.close();

base_dir="."
os.path.walk(base_dir,cleanBrds,None)
os.path.walk(base_dir,sanBrds,None)
# python prepbrd.py
checking .
checking ./naut
checking .
found erct.brd
checking ./naut
found nautctrl.brd
Traceback (most recent call last):
  File "prepbrd.py", line 43, in <module>
    os.path.walk(base_dir,sanBrds,None)
  File "/usr/lib/python2.6/posixpath.py", line 236, in walk
    walk(name, func, arg)
  File "/usr/lib/python2.6/posixpath.py", line 228, in walk
    func(arg, top, names)
  File "prepbrd.py", line 22, in sanBrds
    tree=etree.parse(file)
  File "lxml.etree.pyx", line 2706, in lxml.etree.parse (src/lxml/lxml.etree.c:49958)
  File "parser.pxi", line 1500, in lxml.etree._parseDocument (src/lxml/lxml.etree.c:71797)
  File "parser.pxi", line 1529, in lxml.etree._parseDocumentFromURL (src/lxml/lxml.etree.c:72080)
  File "parser.pxi", line 1429, in lxml.etree._parseDocFromFile (src/lxml/lxml.etree.c:71175)
  File "parser.pxi", line 975, in lxml.etree._BaseParser._parseDocFromFile (src/lxml/lxml.etree.c:68173)
  File "parser.pxi", line 539, in lxml.etree._ParserContext._handleParseResultDoc

(src/lxml/lxml.etree.c:64257) 文件“parser.pxi”,第 625 行,在 lxml.etree._handleParseResult (src/lxml/lxml.etree.c:65178) 文件“parser.pxi”,第 563 行,在 lxml.etree._raiseParseError (src/lxml/lxml.etree.c:64493) IOError:读取文件“nautctrl.brd”时出错:无法加载外部实体“nautctrl.brd”

我对此很陌生,但似乎我可能没有正确地将参数从 os.path.walk 传递给其他函数。我想先删除所有以 _pyclean.brd 结尾的文件,然后对剩余的 .brd 文件(即 xml)进行操作。这些函数在平面目录中工作正常,但递归我得到上述错误。

【问题讨论】:

    标签: python


    【解决方案1】:

    files 中的文件名是短名称。您想使用可以使用os.path.join 构建的完整路径:

    def sanBrds(args, dirname, files):
        print "checking ", dirname
        for file in files:
            fullpath = os.path.join(dirname, file)
            if fullpath.endswith(".brd"):
                print "found ", file
                newfile = splitext(basename(fullpath))[0] + "_pyclean.brd"
                tree = etree.parse(fullpath)
                # ...
    

    【讨论】:

      猜你喜欢
      • 2017-05-19
      • 2016-12-21
      • 1970-01-01
      • 1970-01-01
      • 2019-01-06
      • 2016-10-10
      • 1970-01-01
      • 1970-01-01
      • 2022-08-13
      相关资源
      最近更新 更多