On this page
gzip
Prev: grp | Up: stdlib_module | Next: hashlib
Rel: pickle, copyreg, shelve, marshal, dbm
Ref: docs.python.org — gzip
Tags: #public #python3 #stdlib
Import
import gzip
Overview
Reads and writes .gz compressed files and in-memory byte streams (wrapping zlib with standard gzip headers and CRC32 integrity checks). Reach for gzip.open(..., "rt", encoding="utf-8") to stream large compressed logs or JSONL datasets line by line without decompressing the entire file to disk.
Quick Start
import gzip
raw = b"repeated text " * 100
compressed = gzip.compress(raw, compresslevel=6)
assert len(compressed) < len(raw)
assert gzip.decompress(compressed) == raw
Implementation Walkthrough
1. Streaming Compressed Text Files (gzip.open)
import gzip
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
gz_path = Path(tmp) / "events.jsonl.gz"
with gzip.open(gz_path, "wt", encoding="utf-8") as fh:
fh.write('{"id": 1}\n{"id": 2}\n')
with gzip.open(gz_path, "rt", encoding="utf-8") as fh:
lines = [line.strip() for line in fh]
assert lines == ['{"id": 1}', '{"id": 2}']
2. Deterministic Reproducible .gz Output (mtime=0)
By default, gzip embeds the current wall-clock timestamp in the header; pass mtime=0 for reproducible builds and deterministic hashes:
import gzip
a = gzip.compress(b"payload", mtime=0)
b = gzip.compress(b"payload", mtime=0)
assert a == b
Gotchas & Boundaries
- Default mode of
gzip.open()is binary ("rb"), not text: Pass"rt"or"wt"withencoding="utf-8"when reading/writing strings. - Guard against gzip bombs on untrusted input: Stream with bounded
.read(max_bytes)instead of callinggzip.decompress(untrusted_bytes)blindly. - Gzip compresses a single stream, not a directory: Combine with tarfile (
tar.gz) or zipfile to archive multiple files.
Common Functions
- gzip.compress() —
compress(data, compresslevel=9, *, mtime=0)— Compress data in one shot and return the compressed string. - gzip.decompress() —
decompress(data)— Decompress a gzip compressed string in one shot. - gzip.open() —
open(filename, mode='rb', compresslevel=9, encoding=None, errors=None, newline=None)— Open a gzip-compressed file in binary or text mode.
Classes & Objects
- gzip.BadGzipFile — Exception raised in some cases for invalid gzip files. (Methods:
.add_note(),.with_traceback()) - gzip.GzipFile — The GzipFile class simulates most of the methods of a file object with the exception of the truncate() method. (Methods:
.close(),.closed(),.detach(),.fileno(),.flush(),.isatty())
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/grp
- tuts/python3/stdlib_module/gzip/BadGzipFile
- tuts/python3/stdlib_module/gzip/compress
- tuts/python3/stdlib_module/gzip/decompress
- tuts/python3/stdlib_module/gzip/GzipFile
- tuts/python3/stdlib_module/gzip/open
- tuts/python3/stdlib_module/hashlib
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/zlib
Related notes
- tuts/python3/stdlib_module/grp Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/gzip/BadGzipFile Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/gzip/compress Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/gzip/decompress Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/gzip/GzipFile Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/gzip/open Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/hashlib Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/zlib Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib