line-by-line file read

Michael Stenner mstenner at phy.duke.edu
Fri Mar 15 10:05:41 EST 2002


On Fri, Mar 15, 2002 at 05:38:46AM -0800, Paul Rubin wrote:
> 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()

Ahh... thank you, Paul (and others).  I had been looking at the 1.5.2
docs (for good reason).  I've been finding lots of reasons to go
with 2.x lately... looks like I found another one :)

					-Michael
-- 
  Michael Stenner                       Office Phone: 919-660-2513
  Duke University, Dept. of Physics       mstenner at phy.duke.edu
  Box 90305, Durham N.C. 27708-0305




More information about the Python-list mailing list