Testing for EOF ?

Simon Brunning simon.brunning at gmail.com
Mon Apr 4 07:48:00 EDT 2005


On Apr 4, 2005 12:36 PM, Peter Moscatt <pgmoscatt at optushome.com.au> wrote:
> Thanks Simon,
> 
> So the code should look like:
> 
> f=open(myfile,"r")
> 
> for some_var in f:
>   text=f.readline()
>   print text
> 
> Do I have this correct ?

Nearly - you don't need the text=f.readline() bit. A file object (as
returned to you by open()) is an iterator, and iterating over it gives
you the lines directly.

Why not give it a go? The nice thing about Python is how easy it is to
try stuff like this out at the interactive prompt. Err, *one* of the
nice things, that is. ;-)

BTW, newbies are more than welcome here, but there is a python-tutor
list[1] which might be more your speed if you are quite new to Python.
There are also a number of tutorials available[2] . I'd recommend
either the standard tutorial[3] or Dive Into Python[4] for someone
like you who already knows how to program but doesn't know Python.

Welcome to Python, and have fun.

-- 
Cheers,
Simon B,
simon at brunningonline.net,
http://www.brunningonline.net/simon/blog/
[1] http://mail.python.org/mailman/listinfo/tutor
[2] http://www.python.org/moin/BeginnersGuide/Programmers
[3] http://www.python.org/doc/current/tut/
[4] http://diveintopython.org/



More information about the Python-list mailing list