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