os.path.walk -- Can You Limit Directories Returned?

Gary Herron gherron at islandtraining.com
Thu Jun 5 09:41:08 CEST 2008


Jeff Nyman wrote:
> Greetings all.
>
> I did some searching on this but I can't seem to find a specific
> solution. I have code like this:
>
> =========================================
> def walker1(arg, dirname, names):
>     DC_List.append((dirname,''))
>
> os.path.walk('\\\\vcdcflx006\\Flex\\Sites', walker1, 0)
> =========================================
>
> The Sites\ directory is set up like this:
>
> Sites\
>    Baltimore
>    Birmingham
>    ....
>
> And so forth. Each of the city directories has directories under it as
> well. The problem is that my code grabs every single directory that is
> under the various city directories when what I really want it to do is
> just grab the directories that are under Sites\ and that's it. I don't
> want it to recurse down into the sub-directories of the cities.
>
> Is there a way to do this? Or is os.path.walk not by best choice here?
>   

Yes.   But first, use the more modern iterator os.walk instead of the 
older function calling os.path.walk.  Then in either case (or at least 
for the os.walk -- I'm a little rusty on the older os.path.walk) you can 
modify in-place the subdirectory listing that was passed to you, thereby 
controlling which subdirectories the walk follows. 

Here's some examples:

for path, dirs, files in os.walk(root):
    if 'etc' in dirs:
        dirs.remove('etc')    # Skip any directory named 'etc'
    if path == 'whatever':
        del dirs[:]                # Clearing dirs means recurse into NO 
subdirectory of path
    ... process the files of directory path...


Gary Herron

> Any help and/or advice would be appreciated.
>
> - Jeff
> --
> http://mail.python.org/mailman/listinfo/python-list
>   




More information about the Python-list mailing list