On this page
bz2
Prev: builtins | Up: stdlib_module | Next: cProfile
Rel: pickle, copyreg, shelve, marshal, dbm
Ref: docs.python.org — bz2
Tags: #public #python3 #stdlib
Import
import bz2
Overview
bzip2 compression and decompression (compress, decompress, open, BZ2File, BZ2Decompressor), widely used for Wikipedia multistream dumps (.xml.bz2) and legacy .tar.bz2 archives. Supports reading concatenated multistream .bz2 blocks natively.
Quick Start
import bz2
raw = b"wikipedia dump excerpt " * 100
compressed = bz2.compress(raw, compresslevel=9)
assert len(compressed) < len(raw)
assert bz2.decompress(compressed) == raw
Implementation Walkthrough
1. Streaming Multistream .bz2 Files Without Full Extraction
import bz2
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
p = Path(tmp) / "sample.txt.bz2"
with bz2.open(p, "wt", encoding="utf-8") as fh:
fh.write("article 1\narticle 2\n")
with bz2.open(p, "rt", encoding="utf-8") as fh:
assert fh.read().splitlines() == ["article 1", "article 2"]
Gotchas & Boundaries
- Slower than
gzipandcompression.zstd: Prefercompression.zstdorgzipfor new archives; usebz2primarily when consuming existing.bz2datasets. - Default
bz2.open()mode is binary ("rb"): Pass"rt"/"wt"withencoding="utf-8"for text. - Cap decompressed output on untrusted input with
BZ2Decompressor().decompress(chunk, max_length=...).
Common Functions
- bz2.compress() —
compress(data, compresslevel=9)— Compress a block of data. - bz2.decompress() —
decompress(data)— Decompress a block of data. - bz2.open() —
open(filename, mode='rb', compresslevel=9, encoding=None, errors=None, newline=None)— Open a bzip2-compressed file in binary or text mode.
Classes & Objects
- bz2.BZ2Compressor — Create a compressor object for compressing data incrementally. (Methods:
.compress(),.flush()) - bz2.BZ2Decompressor — Create a decompressor object for decompressing data incrementally. (Methods:
.decompress()) - bz2.BZ2File — A file object providing transparent bzip2 (de)compression. (Methods:
.close(),.closed(),.detach(),.fileno(),.flush(),.isatty())
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/builtins
- tuts/python3/stdlib_module/bz2/BZ2Compressor
- tuts/python3/stdlib_module/bz2/BZ2Decompressor
- tuts/python3/stdlib_module/bz2/BZ2File
- tuts/python3/stdlib_module/bz2/compress
- tuts/python3/stdlib_module/bz2/decompress
- tuts/python3/stdlib_module/bz2/open
- tuts/python3/stdlib_module/cProfile
- tuts/python3/stdlib_module/stdlib_module
Related notes
- tuts/python3/stdlib_module/builtins Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/bz2/BZ2Compressor Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/bz2/BZ2Decompressor Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/bz2/BZ2File Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/bz2/compress 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/open Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/cProfile Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib