line-by-line file read

Gerhard Häring gerhard at bigfoot.de
Fri Mar 15 08:27:55 EST 2002


Michael Stenner wrote:
> OK, so the wonderful flow of the "warty range" thread inspired me to
> ask the following style question:
> 
> I often find myself reading and processing a file line-by-line.  The
> way I usually end up doing it goes something like this
> [...]
> Anyway... just wondering if there's a preferred way to do this.

Python 2.2 and later:

for line in open("bla"):
     # do something with line

Pre-2.2 Python:

for line in open("bla").xreadlines():
     # do something with line

Gerhard




More information about the Python-list mailing list