xreadlines (was Re: while true: !!!)
jepler at inetnebr.com
Fri Dec 15 08:58:47 EST 2000
On Thu, 14 Dec 2000 11:14:38 +0100, Alex Martelli
<aleaxit at yahoo.com> wrote:
>...if it was able to iterate over already-opened file-like objects, rather
>than having to open actual files itself, it WOULD conceptually solve it
>all. I wonder how hard it would be to generalize fileinput in this way --
>probably not very (the in-place-rewriting option would be incompatible
>with having already-opened file-like objects in the list, of course).
xreadlines takes a file object, not a filename, as its argument.
>If [using readline] was a performance problem, it could of course
>also be fixed in a future fileinput version without changing code that
>uses it (again, in-place-rewriting would probably have to inhibit the
>optimization, although that isn't entirely clear).
xreadlines internally uses fp.readlines(sizehint), so
for line in xreadlines.xreadlines(fp): pass
is just about as fast as
for line in fp.readlines(): pass
except that it will not require storage for the whole file.
More information about the Python-list