By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,587 Members | 1,575 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,587 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
<ja*************@gmail.com> wrote:
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>

Jul 19 '05 #2

P: n/a
fuzzylollipop wrote:
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

Jul 19 '05 #3

P: n/a
I am running 2.3.5

Jul 19 '05 #4

P: n/a
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 ) :-(

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.