pydoc > bz2

📘 NAME

bz2 - Interface to the libbzip2 compression library.

🚀 Quick Reference

Use CaseCommandDescription
Compress a block of databz2.compress(data, compresslevel=9)One-shot compression of bytes
Decompress a block of databz2.decompress(data)One-shot decompression of bytes
Open a bzip2 file (binary or text mode)bz2.open(filename, mode='rb', compresslevel=9)Returns a file-like object
Read/write files transparentlywith BZ2File('file.bz2', 'r') as f:File interface for bzip2
Compress incrementallycompressor = BZ2Compressor(); compressor.compress(data); compressor.flush()Incremental compression
Decompress incrementallydecompressor = BZ2Decompressor(); decompressor.decompress(data)Incremental decompression

📂 MODULE REFERENCE

https://docs.python.org/3.10/library/bz2.html

The following documentation is automatically generated from the Python source files. It may be incomplete, incorrect or include features that are considered implementation detail and may vary between Python implementations. When in doubt, consult the module reference at the location listed above.

📝 DESCRIPTION

This module provides a file interface, classes for incremental (de)compression, and functions for one-shot (de)compression.

📦 CLASSES

_compression.BaseStream(io.BufferedIOBase)
    BZ2File
builtins.object
    _bz2.BZ2Compressor
    _bz2.BZ2Decompressor

🔧 class BZ2Compressor

class BZ2Compressor(builtins.object)
 |  BZ2Compressor(compresslevel=9, /)
 |
 |  Create a compressor object for compressing data incrementally.
 |
 |    compresslevel
 |      Compression level, as a number between 1 and 9.
 |
 |  For one-shot compression, use the compress() function instead.

Methods defined here:

Static methods defined here:

🔧 class BZ2Decompressor

class BZ2Decompressor(builtins.object)
 |  Create a decompressor object for decompressing data incrementally.
 |
 |  For one-shot decompression, use the decompress() function instead.

Methods defined here:

Static methods defined here:

Data descriptors defined here:

📁 class BZ2File

class BZ2File(_compression.BaseStream)
 |  BZ2File(filename, mode='r', *, compresslevel=9)
 |
 |  A file object providing transparent bzip2 (de)compression.
 |
 |  A BZ2File can act as a wrapper for an existing file object, or refer
 |  directly to a named file on disk.
 |
 |  Note that BZ2File provides a *binary* file interface - data read is
 |  returned as bytes, and data to be written should be given as bytes.

Method resolution order:

BZ2File
_compression.BaseStream
io.BufferedIOBase
_io._BufferedIOBase
io.IOBase
_io._IOBase
builtins.object

Methods defined here:

Readonly properties defined here:

Data and other attributes defined here:

Methods inherited from _io._BufferedIOBase:

Methods inherited from _io._IOBase:

Static methods inherited from _io._IOBase:

Data descriptors inherited from _io._IOBase:

⚙️ FUNCTIONS

📌 compress

compress(data, compresslevel=9)
    Compress a block of data.

    compresslevel, if given, must be a number between 1 and 9.

    For incremental compression, use a BZ2Compressor object instead.

📌 decompress

decompress(data)
    Decompress a block of data.

    For incremental decompression, use a BZ2Decompressor object instead.

📌 open

open(filename, mode='rb', compresslevel=9, encoding=None, errors=None, newline=None)
    Open a bzip2-compressed file in binary or text mode.

    The filename argument can be an actual filename (a str, bytes, or
    PathLike object), or an existing file object to read from or write
    to.

    The mode argument can be "r", "rb", "w", "wb", "x", "xb", "a" or
    "ab" for binary mode, or "rt", "wt", "xt" or "at" for text mode.
    The default mode is "rb", and the default compresslevel is 9.

    For binary mode, this function is equivalent to the BZ2File
    constructor: BZ2File(filename, mode, compresslevel). In this case,
    the encoding, errors and newline arguments must not be provided.

    For text mode, a BZ2File object is created, and wrapped in an
    io.TextIOWrapper instance with the specified encoding, error
    handling behavior, and line ending(s).

📊 DATA

__all__ = ['BZ2File', 'BZ2Compressor', 'BZ2Decompressor', 'open', 'com...

👤 AUTHOR

Nadeem Vawda <nadeem.vawda AT gmail.com>

📁 FILE

/usr/lib/python3.10/bz2.py
bz2
📘 NAME 🚀 Quick Reference 📂 MODULE REFERENCE 📝 DESCRIPTION 📦 CLASSES
🔧 class BZ2Compressor 🔧 class BZ2Decompressor 📁 class BZ2File
⚙️ FUNCTIONS
📌 compress 📌 decompress 📌 open
📊 DATA 👤 AUTHOR 📁 FILE

Generated by phpman v4.9.27 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-19 19:04 @216.73.216.114
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 Transitional!Valid CSS!
Enhanced by LLM: deepseek-v4-flash / taotoken.net / www.chedong.com - original format

^_top_^