os.walk() python: 目录结构的 XML 表示,递归
我正在尝试使用 os.walk() 来生成一个目录结构的 XML 表示。结果我发现出现了很多重复的内容。最开始的时候,它能正确地把目录放在彼此里面,把文件放在正确的位置;但是在正确处理了一部分后,它又开始错误地遍历了。我不太明白为什么会这样……
这是我的代码:
def dirToXML(self,directory):
curdir = os.getcwd()
os.chdir(directory)
xmlOutput=""
tree = os.walk(directory)
for root, dirs, files in tree:
pathName = string.split(directory, os.sep)
xmlOutput+="<dir><name><![CDATA["+pathName.pop()+"]]></name>"
if len(files)>0:
xmlOutput+=self.fileToXML(files)
for subdir in dirs:
xmlOutput+=self.dirToXML(os.path.join(root,subdir))
xmlOutput+="</dir>"
os.chdir(curdir)
return xmlOutput
fileToXML 这个函数只是把列表解析出来,所以不用担心这个。
目录结构很简单:
images/
images/testing.xml
images/structure.xml
images/Hellos
images/Goodbyes
images/Goodbyes/foo
images/Goodbyes/bar
images/Goodbyes/square
而生成的 XML 文件变成了:
<structure>
<dir>
<name>images</name>
<files>
<file>
<name>structure.xml</name>
</file>
<file>
<name>testing.xml</name>
</file>
</files>
<dir>
<name>Hellos</name>
</dir>
<dir>
<name>Goodbyes</name>
<dir>
<name>foo</name>
</dir>
<dir>
<name>bar</name>
</dir>
<dir>
<name>square</name>
</dir>
</dir>
<dir>
<name>foo</name>
</dir>
<dir>
<name>bar</name>
</dir>
<dir>
<name>square</name>
</dir>
</dir>
<dir>
<name>Hellos</name>
</dir>
<dir>
<name>Goodbyes</name>
<dir>
<name>foo</name>
</dir>
<dir>
<name>bar</name>
</dir>
<dir>
<name>square</name>
</dir>
</dir>
<dir>
<name>foo</name>
</dir>
<dir>
<name>bar</name>
</dir>
<dir>
<name>square</name>
</dir>
</structure>
任何帮助都会非常感激!
3 个回答
0
我在尝试使用os.walk这个功能时,发现它不适合我想要在xml中创建的递归树结构。于是我对我的代码进行了修改,结果得到了我需要的效果:
def dirToXML(self,directory):
curdir = os.getcwd()
os.chdir(directory)
xmlOutput=""
pathName = string.split(directory, os.sep)
xmlOutput+="<dir><name><![CDATA["+pathName.pop()+"]]></name>"
for item in os.listdir(directory):
if os.path.isfile(os.path.join(directory, item)):
xmlOutput+="<file><name><![CDATA["+item+"]]></name></file>"
else :
xmlOutput+=self.dirToXML(os.path.join(directory,item))
xmlOutput+="</dir>"
os.chdir(curdir)
return xmlOutput
6
删除这两行:
for subdir in dirs:
xmlOutput+=self.dirToXML(os.path.join(root,subdir))
你在递归进入子目录,但这是多余的,因为 os.walk 自己就会进行递归。
9
我不建议使用 os.walk()
,因为它的输出需要你做很多处理才能用。相反,建议使用一个递归函数,利用 os.listdir()
、os.path.join()
、os.path.isdir()
等函数来实现。
import os
from xml.sax.saxutils import escape as xml_escape
def DirAsXML(path):
result = '<dir>\n<name>%s</name>\n' % xml_escape(os.path.basename(path))
dirs = []
files = []
for item in os.listdir(path):
itempath = os.path.join(path, item)
if os.path.isdir(itempath):
dirs.append(item)
elif os.path.isfile(itempath):
files.append(item)
if files:
result += ' <files>\n' \
+ '\n'.join(' <file>\n <name>%s</name>\n </file>'
% xml_escape(f) for f in files) + '\n </files>\n'
if dirs:
for d in dirs:
x = DirAsXML(os.path.join(path, d))
result += '\n'.join(' ' + line for line in x.split('\n'))
result += '</dir>'
return result
if __name__ == '__main__':
print '<structure>\n' + DirAsXML(os.getcwd()) + '\n</structure>'
就我个人而言,我建议使用一种更简洁的XML结构,把名字放在属性里,去掉 <files>
这个组:
import os
from xml.sax.saxutils import quoteattr as xml_quoteattr
def DirAsLessXML(path):
result = '<dir name=%s>\n' % xml_quoteattr(os.path.basename(path))
for item in os.listdir(path):
itempath = os.path.join(path, item)
if os.path.isdir(itempath):
result += '\n'.join(' ' + line for line in
DirAsLessXML(os.path.join(path, item)).split('\n'))
elif os.path.isfile(itempath):
result += ' <file name=%s />\n' % xml_quoteattr(item)
result += '</dir>'
return result
if __name__ == '__main__':
print '<structure>\n' + DirAsLessXML(os.getcwd()) + '\n</structure>'
这样输出的结果会像:
<structure>
<dir name="local">
<dir name=".hg">
<file name="00changelog.i" />
<file name="branch" />
<file name="branch.cache" />
<file name="dirstate" />
<file name="hgrc" />
<file name="requires" />
<dir name="store">
<file name="00changelog.i" />
等等。
如果 os.walk()
的工作方式更像 expat
的回调函数,那你会轻松很多。