By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,781 Members | 1,345 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,781 IT Pros & Developers. It's quick & easy.

How does os.walk work?

P: n/a
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.

Thanks,

Greg

Jun 7 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
gr********@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

Jun 7 '07 #2

P: n/a
On Jun 7, 5:13 pm, Gary Herron <gher...@islandtraining.comwrote:
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.
Thanks, that does make sense now.

-Greg
Jun 8 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.