line-by-line file read

Paul Rubin phr-n2002a at nightsong.com
Fri Mar 15 08:38:46 EST 2002


Michael Stenner <mstenner at phy.duke.edu> writes:
> I often find myself reading and processing a file line-by-line.  The
> way I usually end up doing it goes something like this
> 
>     fo = open(filename)
>     line = fo.readline()
>     while line:
> 	# do the processing on "line" here
>         line = fo.readline()
>     fo.close()
> ...
> Anyway... just wondering if there's a preferred way to do this.

fo = open(filename)
for line in fo.xreadlines():
   # process the line
fo.close()



More information about the Python-list mailing list