The `bz2file` library provides a file-like object for reading and writing bzip2-compressed files, backporting the `io.BZ2File` interface introduced in Python 3.3. While Python 3.3+ includes `bz2.open()` in its standard library, this standalone package serves as a drop-in replacement or for use with older Python versions (>=2.6, excluding 3.0-3.2). The current version is 0.98. It has an infrequent release cadence, being a stable backport.
pip install bz2fileVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create and read a bzip2-compressed file using `BZ2File` in binary mode. It writes byte data, then reads it back to verify integrity.
Use `import bz2; with bz2.open('file.bz2', 'wb') as f:` for standard library usage.Encode string data to bytes explicitly before writing, e.g., `f.write('my string'.encode('utf-8'))`.For text, use `BZ2File('file.bz2', 'wt', encoding='utf-8')` for writing or `BZ2File('file.bz2', 'rt', encoding='utf-8')` for reading.Convert the string to bytes before writing: `f.write("my data".encode('utf-8'))`.Install the package using pip: `pip install bz2file`.
Ensure the file being opened is a correctly compressed bzip2 file. This error can also occur if the file is corrupted or truncated. Use standard file opening functions for uncompressed files.
No dependency data recorded yet.