os.walk() maxdepth

Tim Peters tim.peters at gmail.com
Mon Aug 2 22:39:55 CEST 2004


]GMTaglia <liquid at nospamkuht.it>]
> I've googled a lot but I couldn't figure out how to implement a maxdepth in
> os.walk(), does anybody had the necessity to do something like that?

No, but

    for root, dirs, files in os.walk(whatever):
        ....
        if root.count(os.sep) >= CUTOFF_DEPTH:
            del dirs[:]

will prune the search based on the number of separator characters in
the directory currently being visited.  Mutating dirs in-place
controls which directories the os.walk generator looks at when it's
resumed.  This is only effective in a top-down walk (which is the
default).



More information about the Python-list mailing list