Python Basics: Reading and Writing Compressed Text with gzip
Python’s standard-library gzip module reads and writes compressed text, including logs and exports saved as .gz files.
Use Python 3.12 or newer in Bash on Linux. Examples run independently and use automatically cleaned temporary directories.
Write and read UTF-8 text
Save as demo_gzip_1.py:
import gzip
from pathlib import Path
from tempfile import TemporaryDirectory
with TemporaryDirectory() as directory:
path = Path(directory) / "notes.txt.gz"
with gzip.open(path, "wt", encoding="utf-8") as output:
output.write("Hello, café!\n")
with gzip.open(path, "rt", encoding="utf-8") as source:
print(source.read(), end="")Run it from the directory where you saved it:
python3 demo_gzip_1.pyOutput:
Hello, café!The t selects text mode, so reads and writes use strings. Without it, the default mode is binary. Set the encoding explicitly. Closing the writer’s with block finishes the file before reading starts. Mode wt replaces existing files; choose real output paths carefully.
Process a compressed log line by line
Save as demo_gzip_2.py:
import gzip
from pathlib import Path
from tempfile import TemporaryDirectory
with TemporaryDirectory() as directory:
path = Path(directory) / "events.log.gz"
with gzip.open(path, "wt", encoding="utf-8") as output:
output.write("INFO started\nERROR timeout\nINFO finished\n")
with gzip.open(path, "rt", encoding="utf-8") as source:
for number, line in enumerate(source, start=1):
if line.startswith("ERROR "):
print(f"Line {number}: {line.rstrip()}")Run it from the directory where you saved it:
python3 demo_gzip_2.pyOutput:
Line 2: ERROR timeoutIteration avoids loading the whole log into one string, though a very long line still needs memory. Limit resources for untrusted input: compressed data may expand enormously.
Compress a small string in memory
Save as demo_gzip_3.py:
import gzip
original = "red\nblue\n"
packed = gzip.compress(original.encode("utf-8"), mtime=0)
restored = gzip.decompress(packed).decode("utf-8")
print("Compressed type:", type(packed).__name__)
print("Restored lines:", restored.splitlines())
print("Round trip:", restored == original)Run it from the directory where you saved it:
python3 demo_gzip_3.pyOutput:
Compressed type: bytes
Restored lines: ['red', 'blue']
Round trip: TrueThese functions work with bytes: encode before compression and decode after decompression. mtime=0 removes the current timestamp from the header, but compressed bytes can still differ across Python or compression-library versions. Both results stay in memory, so use small inputs.
References
Acknowledgements
This post was created with assistance from AI.