470,582 Members | 1,990 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,582 developers. It's quick & easy.

Hack with os.walk()


Have a look at this recursive function:

def walkDirectory( directory, element ):

element = element.newChild( None, "directory", None ) # automatically
appends to parent
element.setProp( "name", os.path.basename(directory))

for root, dirs, files in os.walk( directory ):

for fileName in files:
element.addChild( parseFile( os.path.join( root, fileName ))

for dirName in filter( acceptDirectory, dirs):
walkDirectory( os.path.join( root, dirName ), element )

return ### Note, this is inside for loop

What it does, is it recurses through all directories, and, with libxml2's
bindings, builds an XML document which maps directly to the file hierarchy.
For every file is parseFile() called, which returns a "file element" which is
appended; the resulting structure looks the way one expect -- like a GUI tree

The current code works, but I find it hackish, and it probably is inefficient,
considering that os.walk() is not called once(as it usually is), but for
every directory level.

My problem, AFAICT, with using os.walk() the usual way, is that in order to
construct the /hierarchial/ XML document, I need to be aware of the directory
depth, and a recursive function handles that nicely; os.walk() simply
concentrates on figuring out paths to all files in a directory, AFAICT.

I guess I could solve it with using os.walk() in a traditional way, by somehow
pushing libxml2 nodes on a stack, after keeping track of the directory levels
etc(string parsing..). Or, one could write ones own recursive directory

My question is: what is the least ugly? What is the /proper/ solution for my
problem? How would you write it in the cleanest way?


Jul 18 '05 #1
0 1201

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Marcello Pietrobon | last post: by
6 posts views Thread by rbt | last post: by
7 posts views Thread by KraftDiner | last post: by
6 posts views Thread by Bruce | last post: by
9 posts views Thread by silverburgh.meryl | last post: by
2 posts views Thread by gregpinero | last post: by
2 posts views Thread by Martin Marcher | last post: by
reply views Thread by Jeff McNeil | last post: by
4 posts views Thread by Jeff Nyman | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.