Name

GzipFile

Synopsis

class GzipFile(filename=None,mode=None,compresslevel=9,
               fileobj=None)

Creates and returns a file-like object f that wraps the file or file-like object fileobj. f supplies all methods of built-in file objects except seek and tell. Thus, f is not seekable: you can only access f sequentially, whether for reading or writing. When fileobj is None, filename must be a string that names a file: GzipFile opens that file with the given mode (by default, 'rb'), and f wraps the resulting file object. mode should be one of 'ab', 'rb', 'wb', or None. If mode is None, f uses the mode of fileobj if it is able to find out the mode; otherwise it uses 'rb‘. If filename is None, f uses the filename of fileobj if able to find out the name; otherwise it uses ''. compresslevel is an integer between 1 and 9: 1 requests modest compression but fast operation, and 9 requests the best compression feasible, even if that requires more computation.

File-like object f generally delegates all methods to the underlying file-like object fileobj, transparently accounting for compression as needed. However, f does not allow non-sequential access, so f does not supply methods seek and tell. Moreover, calling f .close does not close fileobj when f was created with an argument fileobj that is not None. This behavior of f .close is very important when fileobj is an instance of StringIO.StringIO, since it means you can call fileobj .getvalue after f .close to get the compressed data as a string. This ...

Get Python in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.