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

alex23 wuwei23 at gmail.com
Thu Jun 5 03:56:22 EDT 2008


On Jun 5, 4:54 pm, Jeff Nyman <jeffny... at gmail.com> wrote:
> 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?

No, os.path.walk will always recurse through all of the sub, that's
its purpose. os.walk produces a generator, which you can then manually
step through if you wish:

    _, DC_List, _ = os.walk('\\\\vcdcflx006\\Flex\\Sites\\*\\').next()

But I'd recommend checking out the glob module:

    from glob import glob
    DC_List = glob('\\\\vcdcflx006\\Flex\\Sites\\*\\')



More information about the Python-list mailing list