How does os.walk work?

G

gregpinero

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
 
G

Gary Herron

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
 
G

gregpinero

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top