xreadlines (was Re: while true: !!!)
aleaxit at yahoo.com
Thu Dec 14 11:14:38 CET 2000
"Greg Jorgensen" <gregj at pobox.com> wrote in message
news:f3%Z5.146409$U46.4768601 at news1.sttls1.wa.home.com...
> There is the fileinput module, which also solves the "ugly infinite loop"
> problem that seems to bother some people:
Or rather, it WOULD solve it, *IF*...:
> import fileinput
> for line in fileinput.input(filename):
> print line
> The fileinput module can iterate over a single file, a list of files, or
...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).
> stdin. I've used fileinput to go through big lists of files (10,000+ email
> messages) and it works great. It doesn't appear to do any buffering
> itself--it uses file.readline() to read the files.
If this 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).
More information about the Python-list