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:
>>> 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
.gzfiles with an interface modeled after the built-inopen() - Supports binary and text modes through
gzip.open() - Compresses and decompresses bytes objects in memory with
gzip.compress()andgzip.decompress() - Allows tuning the compression level from
0(no compression) to9(best compression) - Handles multi-member gzip streams when decompressing
- Integrates with context managers through the
withstatement 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:
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:
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:
$ python read_log.py
Plain text goes in
and gets compressed.
Choosing a faster compression level when working in memory:
>>> 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
.gzlog 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
gzipandgunziptools - 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:
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.
Related Resources
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.
For additional information on related topics, take a look at the following resources:
- Reading and Writing Files in Python (Guide) (Tutorial)
- Bytes Objects: Handling Binary Data in Python (Tutorial)
- Python's with Statement: Manage External Resources Safely (Tutorial)
- How to Download Files From URLs With Python (Tutorial)
- Working With JSON Data in Python (Tutorial)
- Reading and Writing CSV Files in Python (Tutorial)
- Manipulating ZIP Files With Python (Course)
- Reading and Writing Files in Python (Course)
- Reading and Writing Files in Python (Quiz)
- Python Bytes (Quiz)
- Context Managers and Using Python's with Statement (Course)
- Context Managers and Python's with Statement (Quiz)
- Downloading Files From URLs With Python (Course)
- Working With JSON in Python (Course)
- Working With JSON Data in Python (Quiz)
- Reading and Writing CSV Files (Course)
- Reading and Writing CSV Files in Python (Quiz)
By Leodanis Pozo Ramos • Updated April 22, 2026