Home

Deivys Ramirez wrote:
> Hi everyone,
>
> I'm a Python newbie but have experience programming PHP and C so i'm
> not really new to programming but new to Python.
>
> What's the "pythonic" way of reading a text file, line by line,
> without throwing an exception when there's nothing to be read?
>
> I've been trying to map my while(!eof(fp)) mindset to the file object
> Python gives me when I call open() but have had no good results.
>
> Regards,
>
> Deivys Ramirez
> Lima-Peru

File objects are iterable so you you can just loop on them.

fp=open(filename, 'r')
for line in fp:
# do something with line

fp.close()

-Larry Bates

previous
next

APL2007 reminder: early (cheaper) registration ends Thursday 9/13
Parallel/Multiprocessing script design question
Re: C/C++ guidelines
Re: function to do dynamic import?
Re: Class with methods written in asm (Linux)
Dzieci Niczyje
Podaruj Zycie
Akogo
Kidprotect
Pajacyk