ls files --> list packer

Singletoned singletoned at gmail.com
Fri Feb 24 05:28:49 EST 2006


Try using The Path module:
http://www.jorendorff.com/articles/python/path/.

I wrote a little script to traverse a directory structure which you
could use.  (You just pass a function to it and it runs it on each file
in the directory.  You want it to run on each directory instead, so
I've changed it a little for you).

import path

def traverse(directory, function, depth=0, onfiles=True, ondirs=False):
       thedir = path.path(directory)
       if onfiles == True:
              for item in thedir.files():
                      function(item, depth)
       if ondirs == True:
              for item in thedir.dirs():
                     function(item, depth)
       for item in thedir.dirs():
               traverse(item, function, depth+1, onfiles, ondirs)

You can use it like so:

def printaifs(thedir, depth):
       print thedir.name
       for item in thedir.files("*.aif'"):
              print "\t" + item

traverse(r"/Users/foo/snd", printaifs, onfiles=False, ondirs=True)

NB: I've quickly adapted this whilst away from an installation of
Python, so it is untested, but should mostly work, unless there's a
typo.

Hope this helps at least a little...

Ed

PS The Python Tutor list tends to be a much better place to discuss
this kind of stuff.  If you haven't yet encountered Kent and Alan's
help, then you have a joyous experience ahead of you.




More information about the Python-list mailing list