Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

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

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


jeffnyman at gmail

Jun 4, 2008, 11:54 PM

Post #1 of 5 (225 views)
Permalink
os.path.walk -- Can You Limit Directories Returned?

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?

Any help and/or advice would be appreciated.

- Jeff
--
http://mail.python.org/mailman/listinfo/python-list


deets at nospam

Jun 5, 2008, 12:00 AM

Post #2 of 5 (213 views)
Permalink
Re: os.path.walk -- Can You Limit Directories Returned? [In reply to]

Jeff Nyman schrieb:
> 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?
>
> Any help and/or advice would be appreciated.

look into the modules glob and os.

Diez
--
http://mail.python.org/mailman/listinfo/python-list


gherron at islandtraining

Jun 5, 2008, 12:41 AM

Post #3 of 5 (211 views)
Permalink
Re: os.path.walk -- Can You Limit Directories Returned? [In reply to]

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
>

--
http://mail.python.org/mailman/listinfo/python-list


wuwei23 at gmail

Jun 5, 2008, 12:56 AM

Post #4 of 5 (210 views)
Permalink
Re: os.path.walk -- Can You Limit Directories Returned? [In reply to]

On Jun 5, 4:54 pm, Jeff Nyman <jeffny...@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\\*\\')
--
http://mail.python.org/mailman/listinfo/python-list


jeffnyman at gmail

Jun 5, 2008, 5:14 AM

Post #5 of 5 (196 views)
Permalink
Re: os.path.walk -- Can You Limit Directories Returned? [In reply to]

Thank you to everyone for your help.

Much appreciated. I now have a better understanding of how glob can be
used and I have a much better understanding of using the more
effective os.walk.


- Jeff
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.