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

Discussion in 'Python' started by fuzzylollipop, Apr 15, 2005.

  1. 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.
     
    fuzzylollipop, Apr 15, 2005
    #1
    1. Advertising

  2. Re: How to fix this error in the example in 6.1.4 Files andDirectories

    <> 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>
     
    Fredrik Lundh, Apr 15, 2005
    #2
    1. Advertising

  3. fuzzylollipop

    Peter Otten Guest

    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
     
    Peter Otten, Apr 15, 2005
    #3
  4. I am running 2.3.5
     
    fuzzylollipop, Apr 15, 2005
    #4
  5. 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 ) :-(
     
    fuzzylollipop, Apr 15, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page