Add iterator to gzip files?

Russell E. Owen owen at astrono.junkwashington.emu
Mon Feb 11 13:44:58 EST 2002


Anyone know of plans to add an iterator to the gzip module, so one can 
write:
for line in gzipfile:
    ...

It's trivial to change the class code or code up a subclass, e.g.:

class FixGZip(gzip.GzipFile):
    """the GzipFile class normally does not have an iterator; add one"""
    def __iter__(self):
        return self

    def next(self):
        line = self.readline()
        if line:
            return line
        else:
            raise StopIteration

def gzopen(filename, mode="r"):
    return FixGZip(filename, mode)

Similarly, I'm wondering if sockets will, or do, support iteration to 
read lines of data. (I've also wondered why they don't directly support 
read and write as unix sockets do; one can create a file from a socket, 
but that seems a bit indirect.)

-- Russell



More information about the Python-list mailing list