Exclude Directories from os.walk
Scott David Daniels
Scott.Daniels at Acm.Org
Tue Oct 21 14:15:45 EDT 2008
D wrote:
> Ok, my brain's apparently not working right today.. what I'd like to
> do is allow the user to specify a directory to exclude (ex- "C:\temp
> \test") - then, when os.walk gets to "C:\temp\test", it excludes that
> directory and all its subdirectories (so, "C:\temp\mytest\test" should
> still be recursed). Isn't what's posted above keying just upon the
> final subdirectory (i.e. "test") instead of the full path as a whole?
>
def canonical(path_name):
return os.path.normpath(os.path.abspath(path_name))
forbidden = 'a/b/c'
home, final = os.path.split(canonical(forbidden))
for base, dirs, files in os.walk('wherever'):
if final in dirs and home == canonical(base):
dirs.remove(final)
--Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list