473,378 Members | 1,066 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 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 4002
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jonas | last post by:
This works fine in Win XP but does not work at all in Win 98. Private WithEvents objIExplorer As InternetExplorer I have to do it like this to get it to work in Win 98 Dim objIExplorer As...
0
by: Miguel Cardenas | last post by:
Hello list For some strange reason `mysql_config --...` does not work. If try something like gcc -o progname `mysql_config --cflags` progname.c `mysql_config --libs` (NOTE:...
4
by: rick | last post by:
The following basic script works fine in firefox by not in IE. Can anyone spot the problem? In IE I can only delete the first line but not the lines created by javascript. Also, look at the HTML...
15
by: Generic Usenet Account | last post by:
While I have a very good feel for how inlining works, I fail to see how in the world inlining can work if the inlined function is not described "in place" in a header file, but rather defined in a...
2
by: Harry Haller | last post by:
Why does the following work: // (1) Set new cookies setcookie ("font_type", $type_sel, time()+3600); setcookie ("font_size", $size_sel, time()+3600); // (1) Get most recent cookie $font_type =...
0
by: Jarod_24 | last post by:
How does tabindex work in ASP .net pages I dosen't seem to work quite like in regular forms. and there isn't any TabStop property either. 1 .How do you prevent a control form beign "tabbed"....
2
by: gsherp | last post by:
I can get the Onclick event handler to work when I added a new row using insertRow. It doesn't work in IE or in FF var cellarea = row.insertCell(2); cellarea.style.backgroundColor =...
2
by: DC | last post by:
Hi, why does this not work: <asp:ImageButton id="myButt" runat="server" OnCommand="myButt_kick" ImageUrl="<% =GetMyButtUrl() %>" </asp:ImageButton>
5
by: Pat | last post by:
Hi ASP Gurus, Please help me. Autocomplete works fine when the control does not have the focus when the page load. But when I put the focus on page load, then autocomplete does not work. I even...
1
by: sva0008 | last post by:
i have a auto suggest script that does not work in firefox , works great on IE. /******************************************************* AutoSuggest - a javascript automatic text input...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.