eof?

Steven D. Majewski sdm7g at Virginia.EDU
Wed Aug 15 09:49:59 EDT 2001


On Wed, 15 Aug 2001, Alex Martelli wrote:

> "Dietmar Lang" <dietmar at wohnheim.fh-wedel.de> wrote in message
> news:3B7A4C7C.CF1A3C3A at wohnheim.fh-wedel.de...
> >
> > Hi,
> >
> > > There are several ways.  Best overall, today (Python 2.1):
> > >
> > >     for line in fh.xreadlines():
> > >         process(line)
> >
> > Is xreadlines something I missed or is it a misspelled readlines?
> 
> You missed the introduction of xreadlines, which satisfies
> exactly this objection.  xreadlines is to readlines like xrange
> is to range: it produces its sequence a little at a time (you
> can control the buffersize used if you want) rather than
> making it all in memory at one time, and therefore it may
> be more general if you may need to process sequences that
> are very large (readlines is probably going to be faster if the
> file isn't that large compared to available memory, though).

And in 2.2, file objects can automatically return an iterator 
in the context of a for loop, so you can just say:

	for line in open( filename ):
		process(line)

-- Steve Majewski






More information about the Python-list mailing list