How to fix this error in the example in 6.1.4 Files and Directories

F

fuzzylollipop

the following code is from the Python 2.3.5 docs (
http://www.python.org/doc/2.3.5/lib/os-file-dir.html )

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

every time I try and run it I get the following error

print sum([getsize(join(root,name)) from name in files]),
^

the only change I made was the path supplied to os.walk()
I think this has to do something with the list comprehension syntax
and I am trying to learn that part, using this example which seems to
be broken.
 
F

Fredrik Lundh

the following code is from the Python 2.3.5 docs (
http://www.python.org/doc/2.3.5/lib/os-file-dir.html )

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

every time I try and run it I get the following error

print sum([getsize(join(root,name)) from name in files]),
^

if you're using examples from the 2.3.5 documentation, maybe you
shouldn't run them under 1.5.2...

(the code you included runs just fine on Python 2.3 and 2.4 on my
machine. under Python 2.0 and 2.1 and 2.2, it complains about a
missing "walk" function. in Python 1.5.2, it gives a syntax error that
looks a lot like the one you included, except that you didn't include
the actual error message...)

</F>
 
P

Peter Otten

fuzzylollipop said:
print sum([getsize(join(root, name)) for name in files]),
print sum([getsize(join(root,name)) from name in files]),

Do you see the difference? ... for ... is the correct syntax, ... from ...
is what Python seems to complain about.

Peter
 
F

fuzzylollipop

can't see the forest for the trees, that seems to be the problem,
thanks for pointing out the obvious ( that I couldn't see ) :-(
 

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,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top