On this page
gzip.open
Prev: gzip.GzipFile | Up: gzip
Rel: gzip.BadGzipFile, gzip.compress, gzip.decompress, gzip.GzipFile
Ref: docs.python.org — gzip.open
Tags: #public #python3 #stdlib #gzip
Import
import gzip
from gzip import open
Signature & Overview
gzip.open(filename, mode='rb', compresslevel=9, encoding=None, errors=None, newline=None)
Open a gzip-compressed file in binary or text mode.
Usage
import gzip
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
p = Path(tmp) / "log.txt.gz"
with gzip.open(p, "wt", encoding="utf-8") as f:
f.write("hello gzip")
with gzip.open(p, "rt", encoding="utf-8") as f:
assert f.read() == "hello gzip"
Gotchas
- Default mode is binary (
"rb"): Pass"rt"/"wt"withencoding="utf-8"for text.
REPL Inspect
from gzip import open
help(open)
Linked from
Related notes
- tuts/python3/stdlib_module/gzip/BadGzipFile Linked here; links to this page; shared tags: #gzip, #stdlib
- tuts/python3/stdlib_module/gzip/compress Linked here; links to this page; shared tags: #gzip, #stdlib
- tuts/python3/stdlib_module/gzip/decompress Linked here; links to this page; shared tags: #gzip, #stdlib
- tuts/python3/stdlib_module/gzip/GzipFile Linked here; links to this page; shared tags: #gzip, #stdlib
- tuts/python3/stdlib_module/gzip Linked here; links to this page; shared tags: #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib
- tuts/c/stdlib/assert/assert shared tags: #stdlib
- tuts/c/stdlib/assert/static_assert shared tags: #stdlib
- tuts/c/stdlib/ctype shared tags: #stdlib