class bz2.BZ2File(filename, mode='r', buffering=None, compresslevel=9)
Open a bzip2-compressed file in binary mode.
If filename is a str
or bytes
object, open the named file directly. Otherwise, filename should be a file object, which will be used to read or write the compressed data.
The mode argument can be either 'r'
for reading (default), 'w'
for overwriting, 'x'
for exclusive creation, or 'a'
for appending. These can equivalently be given as 'rb'
, 'wb'
, 'xb'
and 'ab'
respectively.
If filename is a file object (rather than an actual file name), a mode of 'w'
does not truncate the file, and is instead equivalent to 'a'
.
The buffering argument is ignored. Its use is deprecated.
If mode is 'w'
or 'a'
, compresslevel can be a number between 1
and 9
specifying the level of compression: 1
produces the least compression, and 9
(default) produces the most compression.
If mode is 'r'
, the input file may be the concatenation of multiple compressed streams.
BZ2File
provides all of the members specified by the io.BufferedIOBase
, except for detach()
and truncate()
. Iteration and the with
statement are supported.
BZ2File
also provides the following method:
-
peek([n])
-
Return buffered data without advancing the file position. At least one byte of data will be returned (unless at EOF). The exact number of bytes returned is unspecified.
Note
While calling
peek()
does not change the file position of theBZ2File
, it may change the position of the underlying file object (e.g. if theBZ2File
was constructed by passing a file object for filename).New in version 3.3.
Changed in version 3.1: Support for the with
statement was added.
Changed in version 3.3: The fileno()
, readable()
, seekable()
, writable()
, read1()
and readinto()
methods were added.
Changed in version 3.3: Support was added for filename being a file object instead of an actual filename.
Changed in version 3.3: The 'a'
(append) mode was added, along with support for reading multi-stream files.
Changed in version 3.4: The 'x'
(exclusive creation) mode was added.
Changed in version 3.5: The read()
method now accepts an argument of None
.
Please login to continue.