Par*_*ris 5 python xml recursion directory-structure os.walk
所以我试图使用os.walk()来生成目录结构的XML表示.我似乎得到了大量的重复.它正确地将目录放在彼此之间,并将文件放在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
Run Code Online (Sandbox Code Playgroud)
fileToXML只是解析列表,所以不必担心.
目录结构很简单:
images/
images/testing.xml
images/structure.xml
images/Hellos
images/Goodbyes
images/Goodbyes/foo
images/Goodbyes/bar
images/Goodbyes/square
Run Code Online (Sandbox Code Playgroud)
并生成的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>
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感激!
我建议不要使用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>'
Run Code Online (Sandbox Code Playgroud)
就个人而言,我建议使用更简洁的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>'
Run Code Online (Sandbox Code Playgroud)
这给出了如下输出:
<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" />
Run Code Online (Sandbox Code Playgroud)
等等
如果os.walk()
更像expat
是回调,你会有更轻松的时间.
删除两行:
for subdir in dirs:
xmlOutput+=self.dirToXML(os.path.join(root,subdir))
Run Code Online (Sandbox Code Playgroud)
你正在递归到子目录; 但那是多余的,因为os.walk会自行解决.