471,310 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

How does os.walk work?

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
2 3879
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
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.

Similar topics

reply views Thread by Miguel Cardenas | last post: by
15 posts views Thread by Generic Usenet Account | last post: by
2 posts views Thread by Harry Haller | last post: by
reply views Thread by Jarod_24 | last post: by
1 post views Thread by sva0008 | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.