[Tutor] readlinesX

Sean 'Shaleh' Perry shaleh@valinux.com
Mon, 19 Mar 2001 18:05:29 -0800


On Tue, Mar 20, 2001 at 12:09:24AM +0100, Kalle Svensson wrote:
> Sez Shawhan, Douglas (GEAE, GECC):
> > I have been scouting around on the python.org site looking for references
> > to readlineX. Where can I find the whole poop on this function?
> 
> The below applies to the function xreadlines in the module xreadlines, and
> the file object method xreadlines.
> It returns a sequence object, much like the list returned by readlines, only
> it doesn't read all the lines into memory but reads them "on demand"
> (reading ahead a bit for performance, I think).
> without reading the entire file into memory.  This does away with

Yep, that about sums it up.  It is analogous to range v. xrange.  The idea
here is that if you are reading a HUGE file, using readlines() is bad.
Since you almost never know ahead of time how big a file will be (and they
usually grow), readlines() is a memory sink waiting to happen.

As a casual user, all you need to know is that as long as you are using
python 2.x, always use xreadlines() instead of readlines().  Works the same,
looks the same, it is just kinder to the computer.