readlines() and "binary" files

Alex Martelli aleax at
Wed Sep 25 18:13:25 EDT 2002

Justin wrote:
> at a time, so that's rather overkill. If anyone can think of a more
> economical way of doing it (short of defining my own iterator), I'd
> be interested.

Pity you ruled out defining your own iterator -- in Python 2.2.1...:

from __future__ import generators

def splitby(fileobj, splitter, bufsize=8192):
    buf = ''

    while True:
            item, buf = buf.split(splitter, 1)
        except ValueError:
            more =
            if not more: break
            buf += more
            yield item + splitter

    if buf:
        yield buf

I think it's more or less the simplest approach.


More information about the Python-list mailing list