Back to module index
Go to module by name
bz2
Interface to the libbzip2 compression library.
This module provides a file interface, classes for incremental
(de)compression, and functions for one-shot (de)compression.
Classes
BZ2Compressor
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.
compress(self, data, /)
Provide data to the compressor object.
Returns a chunk of compressed data if possible, or b'' otherwise.
When you have finished providing data to the compressor, call the
flush() method to finish the compression process.
flush(self, /)
Finish the compression process.
Returns the compressed data left in internal buffers.
The compressor object may not be used after this method is called.
BZ2Decompressor
Create a decompressor object for decompressing data incrementally.
For one-shot decompression, use the decompress() function instead.
decompress(self, /, data, max_length=-1)
Decompress *data*, returning uncompressed data as bytes.
If *max_length* is nonnegative, returns at most *max_length* bytes of
decompressed data. If this limit is reached and further output can be
produced, *self.needs_input* will be set to ``False``. In this case, the next
call to *decompress()* may provide *data* as b'' to obtain more of the output.
If all of the input data was decompressed and returned (either because this
was less than *max_length* bytes, or because *max_length* was negative),
*self.needs_input* will be set to True.
Attempting to decompress data after the end of stream is reached raises an
EOFError. Any data found after the end of the stream is ignored and saved in
the unused_data attribute.
eof = <member 'eof' of '_bz2.BZ2Decompressor' objects>
True if the end-of-stream marker has been reached.
needs_input = <member 'needs_input' of '_bz2.BZ2Decompressor' objects>
True if more input is needed before more decompressed data can be produced.
unused_data = <member 'unused_data' of '_bz2.BZ2Decompressor' objects>
Data found after the end of the compressed stream.
BZ2File
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.
close(self)
Flush and close the file.
May be called more than once without error. Once the file is
closed, any other operation on it will raise a ValueError.
detach(self, /)
Disconnect this buffer from its underlying raw stream and return it.
After the raw stream has been detached, the buffer is in an unusable
state.
fileno(self)
Return the file descriptor for the underlying file.
flush(self, /)
Flush write buffers, if applicable.
This is not implemented for read-only and non-blocking streams.
isatty(self, /)
Return whether this is an 'interactive' stream.
Return False if it can't be determined.
peek(self, n=0)
Return buffered data without advancing the file position.
Always returns at least one byte of data, unless at EOF.
The exact number of bytes returned is unspecified.
read(self, size=-1)
Read up to size uncompressed bytes from the file.
If size is negative or omitted, read until EOF is reached.
Returns b'' if the file is already at EOF.
read1(self, size=-1)
Read up to size uncompressed bytes, while trying to avoid
making multiple reads from the underlying stream. Reads up to a
buffer's worth of data if size is negative.
Returns b'' if the file is at EOF.
readable(self)
Return whether the file was opened for reading.
readinto(self, b)
Read bytes into b.
Returns the number of bytes read (0 for EOF).
readinto1(self, buffer, /)
readline(self, size=-1)
Read a line of uncompressed bytes from the file.
The terminating newline (if present) is retained. If size is
non-negative, no more than size bytes will be read (in which
case the line may be incomplete). Returns b'' if already at EOF.
readlines(self, size=-1)
Read a list of lines of uncompressed bytes from the file.
size can be specified to control the number of lines read: no
further lines will be read once the total size of the lines read
so far equals or exceeds size.
seek(self, offset, whence=0)
Change the file position.
The new position is specified by offset, relative to the
position indicated by whence. Values for whence are:
0: start of stream (default); offset must not be negative
1: current stream position
2: end of stream; offset must not be positive
Returns the new file position.
Note that seeking is emulated, so depending on the parameters,
this operation may be extremely slow.
seekable(self)
Return whether the file supports seeking.
tell(self)
Return the current file position.
truncate(...)
Truncate file to size bytes.
File pointer is left unchanged. Size defaults to the current IO
position as reported by tell(). Returns the new size.
writable(self)
Return whether the file was opened for writing.
write(self, data)
Write a byte string to the file.
Returns the number of uncompressed bytes written, which is
always the length of data in bytes. Note that due to buffering,
the file on disk may not reflect the data written until close()
is called.
writelines(self, seq)
Write a sequence of byte strings to the file.
Returns the number of uncompressed bytes written.
seq can be any iterable yielding byte strings.
Line separators are not added between the written byte strings.
closed = <property object at 0x7f0226f849f0>
True if this file is closed.
Functions
RLock
RLock(*args, **kwargs)
Factory function that returns a new reentrant lock.
A reentrant lock must be released by the thread that acquired it. Once a
thread has acquired a reentrant lock, the same thread may acquire it again
without blocking; the thread must release it once for each time it has
acquired it.
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).
Modules
io
os