How does os.walk work?

Gary Herron gherron at islandtraining.com
Thu Jun 7 17:13:00 EDT 2007


gregpinero at gmail.com wrote:
> In the example from help(os.walk) it lists this:
>
>     from os.path import join, getsize
>     for root, dirs, files in walk('python/Lib/email'):
>         print root, "consumes",
>         print sum([getsize(join(root, name)) for name in files]),
>         print "bytes in", len(files), "non-directory files"
>         if 'CVS' in dirs:
>             dirs.remove('CVS')  # don't visit CVS directories
>
> What I'm wondering is how does the "dirs.remove('CVS')" line prevent
> os.walk from visiting that directory?  how does the walk function know
> what you do to the dirs variable?  I tried looking at the code in
> os.py but it wasn't clear to me there either.
>   
Simple:  os.walk builds the list to contain all the subdirectories. 
After giving you a chance to modify that list, ow.walk then goes through
the list (whatever contents it has at that point) and visits any
subdirectory. 

I'd guess your trouble with understanding this has to do with wondering
how the modified list gets from your code back to os.walk.  But in fact
they are not two separate lists, but one list, passed by reference from
os.walk into your code. 

Gary Herron
> Thanks,
>
> Greg
>
>   




More information about the Python-list mailing list