Re: How to avoid certain directories when using os.walk?
- From: Sean DiZazzo <half.italian@xxxxxxxxx>
- Date: Fri, 30 Oct 2009 10:50:34 -0700 (PDT)
On Oct 29, 10:17 pm, Chris Rebert <c...@xxxxxxxxxxxx> wrote:
On Thu, Oct 29, 2009 at 9:53 PM, Peng Yu <pengyu...@xxxxxxxxx> wrote:
I don't see a way to avoid walking over directories of certain names
with os.walk. For example, I don't want os.walk return files whose
path include '/backup/'. Is there a way to do so? Otherwise, maybe I
will have to make my own program. Thank you!
Read the docs! (http://docs.python.org/library/os.html#os.walk):
"""
os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])
Generate the file names in a directory tree by walking the tree
either top-down or bottom-up. For each directory in the tree rooted at
directory top (including top itself), it yields a 3-tuple (dirpath,
dirnames, filenames).
[...]
When topdown is True, the caller can modify the dirnames list
in-place (perhaps using del or slice assignment), and walk() will only
recurse into the subdirectories whose names remain in dirnames; this
can be used to prune the search, impose a specific order of visiting,
or even to inform walk() about directories the caller creates or
renames before it resumes walk() again.
"""
They even include a specific code example of how to skip unwanted
subdirectories.
Cheers,
Chris
--http://blog.rebertia.com
You will run into problems however if you want to delete from a tree
while ignoring certain named directories. Directories must be empty
before they can be deleted, so you must use "topdown=False", but to
prune a search you must use "topdown=True". At least I think that was
the problem I had with my deletion script a while back.
~Sean
.
- Follow-Ups:
- Re: How to avoid certain directories when using os.walk?
- From: Gabriel Genellina
- Re: How to avoid certain directories when using os.walk?
- From: Dave Angel
- Re: How to avoid certain directories when using os.walk?
- References:
- Re: How to avoid certain directories when using os.walk?
- From: Chris Rebert
- Re: How to avoid certain directories when using os.walk?
- Prev by Date: Socket MUD client script
- Next by Date: Guest House Karachi Gulshan Johar Dha Clifton North-Nazimabad
- Previous by thread: Re: How to avoid certain directories when using os.walk?
- Next by thread: Re: How to avoid certain directories when using os.walk?
- Index(es):
Relevant Pages
|