all Python 3bz2function
bz2.decompress()
in-memory compression
Decompresses bzip2 data in a single call and returns the original bytes. It understands several concatenated streams.
Syntax
data = bz2.decompress(data)
Damaged or non-bzip2 data raises OSError('Invalid data stream'), not ValueError — catch that. The result is bytes; get text with .decode(). The decompressed size is unlimited: unpack untrusted input with BZ2Decompressor and max_length, or a tiny archive may take gigabytes of memory.
Examples
import bz2
print(bz2.decompress(bz2.compress(b'one ') + bz2.compress(b'two'))) # → b'one two'
try:
bz2.decompress(b'not bzip2')
except OSError as e:
print(e) # → Invalid data stream
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy