This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: invalid syntax in os.walk() first example
Type: Stage:
Components: Documentation Versions: Python 2.5
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: rhettinger, yohell
Priority: normal Keywords:

Created on 2005-09-01 11:55 by yohell, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg26174 - (view) Author: YoHell (yohell) Date: 2005-09-01 11:55
Hi!

The first example code block in the documentation for
os.walk() has invalid syntax in it.

The docs are available here:
http://www.python.org/doc/current/lib/os-file-dir.html#l2h-1466

This is the example code block:
import os
from os.path import join, getsize
for root, dirs, files in os.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

Line 5 has the the invalid syntax:
    print sum(getsize(join(root, name)) for name in files),

The example works if brackets are added like so:
    print sum([getsize(join(root, name)) for name in
files]),

I recommend that someone responsible should make this
update in the docs.

You guys are brilliant. Thanks for a splendid language
with docs to match! 

Cheers!

/Joel Hedlund
PhD student, 
IFM Bioinfomatics, Linköping University
msg26175 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2005-09-01 12:44
Logged In: YES 
user_id=80475

I suspect you are using Py2.4 documentation while running
Py2.3 or earlier.  In Py2.4, the bracketless syntax was
introduced with new semantics which produce a similar result
but run with a generator instead of a list comprehension,
resulting in a significant savings in memory.

msg26176 - (view) Author: YoHell (yohell) Date: 2005-09-02 08:43
Logged In: YES 
user_id=1008220

Yes, you are very correct. ;-) 

Can I suggest putting a note on the updated syntax somewhere
in the vicinity?

Thanks again!
/Joel
History
Date User Action Args
2022-04-11 14:56:12adminsetgithub: 42331
2005-09-01 11:55:44yohellcreate