1

我目前正在学习 python 来自动化我的工作中的一些事情。我需要从目录中的多个 xml 文件中检索标签值。该目录也有许多子文件夹。

我尝试了以下代码,并了解缺少什么。但我无法解决这个问题。这是我的代码:

from xml.dom.minidom import parse, parseString
import os
def jarv(dir):
for r,d,f in os.walk(dir):
    for files in f:
        if files.endswith(".xml"):
            print files
                dom=parse(files)
            name = dom.getElementsByTagName('rev')
            print name[0].firstChild.nodeValue
jarv("/path)

我知道在执行该dom=parse(files)行时,它得到了没有路径的文件名。所以它说没有这样的文件/目录。

我不知道如何解决这个问题。

4

1 回答 1

1

You have to use os.path.join() to build the correct path from the dirname and the filename:

dom=parse(os.path.join(r, files))

should do it

于 2013-05-06T10:57:33.820 回答