iterating over lines in a file

Cliff Crawford cjc26 at nospam.cornell.edu
Sun Jul 23 10:34:57 EDT 2000


* David Bolen <db3l at fitlinxx.com> menulis:
| 
| > That doesn't seem to be true--readlines() reads the whole file whether
| > you specify a size argument or not.  For example:
| 
| You might want to try a larger sample - readlines() is documented that
| the size value is a hint - it might be rounded up to some internal
| buffer size, which I could easily imagine as being a few K.
| 
| At least on Windows, with Python 1.5.2, it appears to work in
| multiples of about 8K.  That is, size values are rounded up to the
| next multiple of 8K, and the total size of information returned will
| be somewhat shy of that multiple depending on line length since it
| only returns full lines.

Ugh, you're right (I checked fileobject.c to make sure).  That makes
readlines() a lot less useful than I thought it was..:(

I guess in that case you would want something like xreadlines() (lazy
readlines) then.  Below is a post I saved from a little over a year ago,
which describes one way to implement xreadlines() and other lazy
iterators.  (I would've provided a link to Deja instead, but it seems
their "archive" doesn't go back far enough to have this post..:P )


-- 
cliff crawford    -><-    http://www.people.cornell.edu/pages/cjc26/
                          Synaesthesia now!            icq 68165166

----------



More information about the Python-list mailing list