On 11/01/06, Liam Clarke <[EMAIL PROTECTED]> wrote:
> Hi Srinivas -
>
> For walking a directory, you can use os.walk() or os.path.walk(), but
> I prefer the path module here -
> http://www.jorendorff.com/articles/python/path/.

The Path module is excellent, but it's walk still doesn't take into
account the depth of the current file in the folder structure.

If you need that, I wrote (with Kent's help) a simple script that will
take it into account (you need the Path module above for it to work).

def traverse(directory, function, depth=0):
        import path
        thedir = path.path(directory)
        for item in thedir.files():
                function(item, depth)
        for item in thedir.dirs():
                traverse(item,function, depth+1)

It can be used like:

def doprint(item, depth):
        print item

traverse(r"C:\Temp", doprint)

Hope it's helpful to someone.

Ed
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to