Python Glossary Русский

all Python 3bz2term

bz2.BZ2Decompressor

streaming compression

An object for streaming bzip2 decompression: it takes compressed data in parts and returns the decompressed bytes.

Syntax

d = bz2.BZ2Decompressor()

One object decompresses exactly one stream: at the end it sets eof=True and puts anything extra into unused_data — unlike bz2.decompress(), which joins several streams itself. Using it again after eof gives EOFError. The max_length parameter caps the output and guards against a decompression bomb.

Examples

import bz2
d = bz2.BZ2Decompressor()
print(d.decompress(bz2.compress(b'data') + b'tail'))  # → b'data'
print(d.eof, d.unused_data)  # → True b'tail'
try:
    d.decompress(b'more')
except EOFError as e:
    print(e)  # → End of stream already reached

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy