On this page
gzip.GzipFile
Prev: gzip.decompress | Up: gzip | Next: gzip.open
Rel: gzip.BadGzipFile, gzip.compress, gzip.decompress, gzip.open
Ref: docs.python.org — gzip.GzipFile
Tags: #public #python3 #stdlib #gzip
Import
import gzip
from gzip import GzipFile
Signature & Overview
gzip.GzipFile(filename=None, mode=None, compresslevel=9, fileobj=None, mtime=None)
The GzipFile class simulates most of the methods of a file object with the exception of the truncate() method.
Methods & Attributes
GzipFile.close(self)—GzipFile.closemethod/property.GzipFile.closed(...)—GzipFile.closedmethod/property.GzipFile.detach(self, /)— Disconnect this buffer from its underlying raw stream and return it.GzipFile.fileno(self)— Invoke the underlying file object's fileno() method.GzipFile.flush(self, zlib_mode=2)—GzipFile.flushmethod/property.GzipFile.isatty(self, /)— Return whether this is an 'interactive' stream.GzipFile.mtime(...)— Last modification time read from stream, or NoneGzipFile.peek(self, n)—GzipFile.peekmethod/property.GzipFile.read(self, size=-1)—GzipFile.readmethod/property.GzipFile.read1(self, size=-1)— Implements BufferedIOBase.read1() Reads up to a buffer's worth of data if size is negative.GzipFile.readable(self)—GzipFile.readablemethod/property.GzipFile.readinto(self, b)—GzipFile.readintomethod/property.GzipFile.readinto1(self, b)—GzipFile.readinto1method/property.GzipFile.readline(self, size=-1)—GzipFile.readlinemethod/property.GzipFile.readlines(self, hint=-1, /)— Return a list of lines from the stream.GzipFile.rewind(self)— Return the uncompressed stream file position indicator to the beginning of the fileGzipFile.seek(self, offset, whence=0)—GzipFile.seekmethod/property.GzipFile.seekable(self)—GzipFile.seekablemethod/property.GzipFile.tell(self)—GzipFile.tellmethod/property.GzipFile.truncate(self, size=None, /)— Truncate file to size bytes.GzipFile.writable(self)—GzipFile.writablemethod/property.GzipFile.write(self, data)—GzipFile.writemethod/property.GzipFile.writelines(self, lines, /)— Write a list of lines to stream.
Usage
import gzip
import io
buf = io.BytesIO()
with gzip.GzipFile(fileobj=buf, mode="wb", mtime=0) as gz:
gz.write(b"deterministic")
buf.seek(0)
with gzip.GzipFile(fileobj=buf, mode="rb") as gz:
assert gz.read() == b"deterministic"
Gotchas
- Pass
mtime=0for reproducible builds: Otherwise the current timestamp is embedded in the gzip header.
REPL Inspect
from gzip import GzipFile
help(GzipFile)
Linked from
Related notes
- tuts/python3/stdlib_module/gzip/BadGzipFile Linked here; links to this page; shared tags: #gzip, #stdlib
- tuts/python3/stdlib_module/gzip/compress Linked here; links to this page; shared tags: #gzip, #stdlib
- tuts/python3/stdlib_module/gzip/decompress Linked here; links to this page; shared tags: #gzip, #stdlib
- tuts/python3/stdlib_module/gzip/open Linked here; links to this page; shared tags: #gzip, #stdlib
- tuts/python3/stdlib_module/gzip Linked here; links to this page; shared tags: #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib
- tuts/c/stdlib/assert/assert shared tags: #stdlib
- tuts/c/stdlib/assert/static_assert shared tags: #stdlib
- tuts/c/stdlib/ctype shared tags: #stdlib