Lazy file.readlines()?

William Tanksley wtanksle at dolphin.openprojects.net
Wed Sep 15 20:19:20 EDT 1999


On Wed, 15 Sep 1999 23:21:02 GMT, Neil Schemenauer wrote:
>Fredrik Lundh <fredrik at pythonware.com> wrote:
>>Hrvoje Niksic <hniksic at srce.hr> wrote:
>>> > - reading a file one line at a time (self.__fp.readline())

>>> I don't see an alternative to this, except to read the whole file at
>>> once, which I am trying to avoid, as the files are large.

>What would be cool is if readlines() returned a lazy sequence
>object (ie. only read as much is needed using a certain block
>size).  This should give the advantages of readlines() without
>the concern about sucking up a huge file all at once.

I wonder -- suppose the official Python were to replace the current
readlines() with this one (or with something similar).  Would it be
possible to preserve all the current behavior (except for the memory
usage, of course)?

That sure would seem nice.  It wouldn't solve all of our problems, but it
would make the most obvious speedup idiom behave well.

-- 
-William "Billy" Tanksley




More information about the Python-list mailing list