PyDocs

Nav

Data Compression and Archiving

BZ2File

bz2

class (source)
  • 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.
Changed in 3.1, 3.3, 3.4, 3.5, 3.6, 3.9, 3.10
  • 3.1: Support for the with statement was added.
  • 3.3: Support was added for filename being a file object instead of an actual filename. The 'a' (append) mode was added, along with support for reading multi-stream files.
  • 3.4: The 'x' (exclusive creation) mode was added.
  • 3.5: The read() method now accepts an argument of None.
  • 3.6: Accepts a path-like object.
  • 3.9: The buffering parameter has been removed. It was ignored and deprecated since Python 3.0. Pass an open file object to control how the file is opened. The compresslevel parameter became keyword-only.
  • 3.10: This class is thread unsafe in the face of multiple simultaneous readers or writers, just like its equivalent classes in gzip and lzma have always been.
Members of BZ2File

f

fileno
Return the file descriptor for the underlying file.

m

mode
'rb' for reading and 'wb' for writing.

n

name
The bzip2 file name.

p

peek
Return buffered data without advancing the file position.

r

read1
Read up to size uncompressed bytes, while trying to avoid making multiple reads from the underlying stream.
readable
Return whether the file was opened for reading.
readinto
Read bytes into b.

s

seekable
Return whether the file supports seeking.

w

writable
Return whether the file was opened for writing.
0 Examples
No examples for bz2/BZ2File.
See Also
No see-alsos for bz2/BZ2File
0 Notes
No notes for BZ2File