Skip to content

gzip

The Python gzip module provides a user-friendly interface for reading and writing files compressed in the gzip format, as well as compressing and decompressing data in memory. It wraps the lower-level zlib library to produce files compatible with the GNU gzip and gunzip programs.

Here is a quick in-memory round trip:

Language: Python
>>> import gzip

>>> blob = gzip.compress(b"Compress me!")
>>> blob[:3]
b'\x1f\x8b\x08'
>>> gzip.decompress(blob)
b'Compress me!'

The first two bytes are the gzip magic number, and the third identifies the deflate compression method.

Key Features

  • Reads and writes .gz files with an interface modeled after the built-in open()
  • Supports binary and text modes through gzip.open()
  • Compresses and decompresses bytes objects in memory with gzip.compress() and gzip.decompress()
  • Allows tuning the compression level from 0 (no compression) to 9 (best compression)
  • Handles multi-member gzip streams when decompressing
  • Integrates with context managers through the with statement for automatic cleanup

Frequently Used Classes and Functions

Object Type Description
gzip.open() Function Opens a gzip-compressed file in binary or text mode
gzip.GzipFile Class Provides a file-like object for reading and writing gzip streams
gzip.compress() Function Compresses a bytes object and returns the gzipped data
gzip.decompress() Function Decompresses gzipped bytes and returns the original data
gzip.BadGzipFile Exception Signals that a file is not a valid gzip stream

Examples

Writing a text file transparently compressed with gzip:

Language: Python Filename: write_log.py
import gzip

with gzip.open("notes.txt.gz", "wt", encoding="utf-8") as f:
    f.write("Plain text goes in\n")
    f.write("and gets compressed.\n")

Reading the same file back into a string:

Language: Python Filename: read_log.py
import gzip

with gzip.open("notes.txt.gz", "rt", encoding="utf-8") as f:
    print(f.read())

Run it on the command line:

Language: Shell
$ python read_log.py
Plain text goes in
and gets compressed.

Choosing a faster compression level when working in memory:

Language: Python
>>> import gzip

>>> payload = b"Real Python reference article." * 100
>>> len(gzip.compress(payload, compresslevel=1))
83
>>> len(gzip.compress(payload, compresslevel=9))
74

Common Use Cases

The most common tasks for gzip include:

  • Reading and writing .gz log files without decompressing them first
  • Shrinking large text datasets such as CSV or JSON dumps before storing or transmitting them
  • Streaming compressed HTTP responses or file downloads
  • Producing archives that interoperate with the Unix gzip and gunzip tools
  • Compressing payloads in memory before sending them over a network

Real-World Example

Consider a scenario where you want to compress an existing plain-text log file into a .gz archive without loading the whole file into memory. You can pair gzip.open() with shutil.copyfileobj() to stream the data through:

Language: Python Filename: compress_log.py
import gzip
import shutil

with open("access.log", "rb") as file_in:
    with gzip.open("access.log.gz", "wb") as file_out:
        shutil.copyfileobj(file_in, file_out)

After running the script, access.log.gz contains the same contents as access.log, but in a form that any gzip-aware tool can read. Because both files are opened as context managers, their handles close automatically when the with blocks exit.

Tutorial

Python's zipfile: Manipulate Your ZIP Files Efficiently

In this guided tutorial, you'll learn how to manipulate ZIP files using Python's zipfile module from the standard library. Through hands-on examples, you'll learn how to read, write, compress, and extract files from your ZIP files quickly.

intermediate python stdlib

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated April 22, 2026