readline() - problem

Ben Finney bignose+hates-spam at benfinney.id.au
Tue Oct 2 07:39:38 EDT 2007


piotr at kolodziejczyk.waw.pl writes:

> import string

Why import 'string' if you're not using it?

> f=open('/test/test.asc','r')
> o=open('/test/out.asc','w')
> for line in f:
>     s= f.readline()

Your line object is already bound to the 'line' name in each
iteration. You need to use that, not attempt to read yet another line
each time.

That is, instead of::

    for line in f:
        foo = f.readline()
        do_interesting_thing(foo)

you should do this::

    for line in f:
        do_interesting_thing(line)

-- 
 \     "The cost of a thing is the amount of what I call life which is |
  `\       required to be exchanged for it, immediately or in the long |
_o__)                                    run."  -- Henry David Thoreau |
Ben Finney



More information about the Python-list mailing list