HOWTO · Python
How to Read gzip File in Python
Read a gzip file in Python with gzip.open(), choose bytes or decoded text, and learn how to write, append to, and decompress .gz files.
On this page
Use Python’s standard-library gzip module to read a .gz file without extracting it first. Open it with gzip.open() in binary mode to get bytes, or in text mode with an explicit encoding to get strings. The examples below also show how to create, append to, and decompress gzip files.
Read a gzip file in Python
A gzip file stores compressed data in the gzip format. gzip.open() presents its contents through a file-like object, so you can read all the data at once or process it line by line. The following example creates a small gzip file and reads its contents as UTF-8 text; creating the input in the same program makes the example self-contained.
Run this example:
import gzip
import tempfile
from pathlib import Path
with tempfile.TemporaryDirectory() as directory:
path = Path(directory) / "notes.txt.gz"
with gzip.open(path, "wt", encoding="utf-8") as compressed:
compressed.write("Python makes gzip files easy to read.\n")
with gzip.open(path, "rt", encoding="utf-8") as compressed:
print(compressed.read(), end="")
Choose binary or text mode
Use rb when the compressed contents are bytes, such as an image or another binary format. Iterating over a binary gzip stream yields byte strings. Use rt when the contents represent text; pass the character encoding explicitly so Python decodes those bytes consistently. The equivalent gzip.open() modes are wb for writing bytes and wt for writing text. A text stream accepts strings, while a binary stream accepts bytes.
For large text files, iterate over the text stream instead of loading the entire contents into memory: for line in compressed:. The context manager closes the gzip stream even if processing stops early. Compression can reduce storage use and transfer size for suitable data, while compression and decompression require CPU time; the amount saved depends on the data.
Write a gzip file in Python
Open a destination with wt to write text or wb to write bytes. The w mode creates a new gzip stream and replaces an existing destination. This example creates both its source text and compressed output, then reads the result to show that the write is usable:
import gzip
import tempfile
from pathlib import Path
with tempfile.TemporaryDirectory() as directory:
path = Path(directory) / "report.txt.gz"
with gzip.open(path, "wt", encoding="utf-8") as compressed:
compressed.write("Daily report\n")
with gzip.open(path, "rt", encoding="utf-8") as compressed:
print(compressed.read(), end="")
Append data to a gzip file
Use at to append text or ab to append bytes. Do not use wb when you need to preserve existing contents: write mode starts a new gzip stream. Appending may add another gzip member to the file; gzip readers can read the concatenated members as one logical stream.
import gzip
import tempfile
from pathlib import Path
with tempfile.TemporaryDirectory() as directory:
path = Path(directory) / "log.txt.gz"
with gzip.open(path, "wt", encoding="utf-8") as compressed:
compressed.write("first entry\n")
with gzip.open(path, "at", encoding="utf-8") as compressed:
compressed.write("second entry\n")
with gzip.open(path, "rt", encoding="utf-8") as compressed:
print(compressed.read(), end="")
Decompress a gzip file in Python
To create a regular, uncompressed file, open the gzip source for reading and the destination for writing, then copy from source to destination. The order matters: the gzip stream is the input, and the ordinary file is the output. Binary mode works for both text and non-text contents and avoids decoding or changing the bytes.
import gzip
import shutil
import tempfile
from pathlib import Path
with tempfile.TemporaryDirectory() as directory:
source = Path(directory) / "archive.txt.gz"
destination = Path(directory) / "archive.txt"
with gzip.open(source, "wb") as compressed:
compressed.write(b"Recovered contents\n")
with gzip.open(source, "rb") as compressed:
with destination.open("wb") as output:
shutil.copyfileobj(compressed, output)
print(destination.read_text(encoding="utf-8"), end="")
Choose text mode when you want decoded strings and know the file’s encoding; choose binary mode when you need the original bytes or are handling non-text data. Use wt or wb to create a compressed file, at or ab to append, and read from the gzip file when extracting its contents to a regular file.