| 1 |
|
| 2 |
:mod:`bz2` --- Compression compatible with :program:`bzip2` |
| 3 |
=========================================================== |
| 4 |
|
| 5 |
.. module:: bz2 |
| 6 |
:synopsis: Interface to compression and decompression routines compatible with bzip2. |
| 7 |
.. moduleauthor:: Gustavo Niemeyer <[email protected]> |
| 8 |
.. sectionauthor:: Gustavo Niemeyer <[email protected]> |
| 9 |
|
| 10 |
|
| 11 |
.. versionadded:: 2.3 |
| 12 |
|
| 13 |
This module provides a comprehensive interface for the bz2 compression library. |
| 14 |
It implements a complete file interface, one-shot (de)compression functions, and |
| 15 |
types for sequential (de)compression. |
| 16 |
|
| 17 |
Here is a summary of the features offered by the bz2 module: |
| 18 |
|
| 19 |
* :class:`BZ2File` class implements a complete file interface, including |
| 20 |
:meth:`~BZ2File.readline`, :meth:`~BZ2File.readlines`, |
| 21 |
:meth:`~BZ2File.writelines`, :meth:`~BZ2File.seek`, etc; |
| 22 |
|
| 23 |
* :class:`BZ2File` class implements emulated :meth:`~BZ2File.seek` support; |
| 24 |
|
| 25 |
* :class:`BZ2File` class implements universal newline support; |
| 26 |
|
| 27 |
* :class:`BZ2File` class offers an optimized line iteration using the readahead |
| 28 |
algorithm borrowed from file objects; |
| 29 |
|
| 30 |
* Sequential (de)compression supported by :class:`BZ2Compressor` and |
| 31 |
:class:`BZ2Decompressor` classes; |
| 32 |
|
| 33 |
* One-shot (de)compression supported by :func:`compress` and :func:`decompress` |
| 34 |
functions; |
| 35 |
|
| 36 |
* Thread safety uses individual locking mechanism. |
| 37 |
|
| 38 |
.. note:: |
| 39 |
Handling of multi-stream bzip2 files is not supported. Modules such as |
| 40 |
`bz2file <https://github.com/nvawda/bz2file>`_ let you overcome this. |
| 41 |
|
| 42 |
|
| 43 |
(De)compression of files |
| 44 |
------------------------ |
| 45 |
|
| 46 |
Handling of compressed files is offered by the :class:`BZ2File` class. |
| 47 |
|
| 48 |
|
| 49 |
.. index:: |
| 50 |
single: universal newlines; bz2.BZ2File class |
| 51 |
|
| 52 |
.. class:: BZ2File(filename[, mode[, buffering[, compresslevel]]]) |
| 53 |
|
| 54 |
Open a bz2 file. Mode can be either ``'r'`` or ``'w'``, for reading (default) |
| 55 |
or writing. When opened for writing, the file will be created if it doesn't |
| 56 |
exist, and truncated otherwise. If *buffering* is given, ``0`` means |
| 57 |
unbuffered, and larger numbers specify the buffer size; the default is |
| 58 |
``0``. If *compresslevel* is given, it must be a number between ``1`` and |
| 59 |
``9``; the default is ``9``. Add a ``'U'`` to mode to open the file for input |
| 60 |
in :term:`universal newlines` mode. Any line ending in the input file will be |
| 61 |
seen as a ``'\n'`` in Python. Also, a file so opened gains the attribute |
| 62 |
:attr:`newlines`; the value for this attribute is one of ``None`` (no newline |
| 63 |
read yet), ``'\r'``, ``'\n'``, ``'\r\n'`` or a tuple containing all the |
| 64 |
newline types seen. Universal newlines are available only when |
| 65 |
reading. Instances support iteration in the same way as normal :class:`file` |
| 66 |
instances. |
| 67 |
|
| 68 |
:class:`BZ2File` supports the :keyword:`with` statement. |
| 69 |
|
| 70 |
.. versionchanged:: 2.7 |
| 71 |
Support for the :keyword:`with` statement was added. |
| 72 |
|
| 73 |
|
| 74 |
.. note:: |
| 75 |
|
| 76 |
This class does not support input files containing multiple streams (such |
| 77 |
as those produced by the :program:`pbzip2` tool). When reading such an |
| 78 |
input file, only the first stream will be accessible. If you require |
| 79 |
support for multi-stream files, consider using the third-party |
| 80 |
:mod:`bz2file` module (available from |
| 81 |
`PyPI <https://pypi.org/project/bz2file>`_). This module provides a |
| 82 |
backport of Python 3.3's :class:`BZ2File` class, which does support |
| 83 |
multi-stream files. |
| 84 |
|
| 85 |
|
| 86 |
.. method:: close() |
| 87 |
|
| 88 |
Close the file. Sets data attribute :attr:`closed` to true. A closed file |
| 89 |
cannot be used for further I/O operations. :meth:`close` may be called |
| 90 |
more than once without error. |
| 91 |
|
| 92 |
|
| 93 |
.. method:: read([size]) |
| 94 |
|
| 95 |
Read at most *size* uncompressed bytes, returned as a string. If the |
| 96 |
*size* argument is negative or omitted, read until EOF is reached. |
| 97 |
|
| 98 |
|
| 99 |
.. method:: readline([size]) |
| 100 |
|
| 101 |
Return the next line from the file, as a string, retaining newline. A |
| 102 |
non-negative *size* argument limits the maximum number of bytes to return |
| 103 |
(an incomplete line may be returned then). Return an empty string at EOF. |
| 104 |
|
| 105 |
|
| 106 |
.. method:: readlines([size]) |
| 107 |
|
| 108 |
Return a list of lines read. The optional *size* argument, if given, is an |
| 109 |
approximate bound on the total number of bytes in the lines returned. |
| 110 |
|
| 111 |
|
| 112 |
.. method:: xreadlines() |
| 113 |
|
| 114 |
For backward compatibility. :class:`BZ2File` objects now include the |
| 115 |
performance optimizations previously implemented in the :mod:`xreadlines` |
| 116 |
module. |
| 117 |
|
| 118 |
.. deprecated:: 2.3 |
| 119 |
This exists only for compatibility with the method by this name on |
| 120 |
:class:`file` objects, which is deprecated. Use ``for line in file`` |
| 121 |
instead. |
| 122 |
|
| 123 |
|
| 124 |
.. method:: seek(offset[, whence]) |
| 125 |
|
| 126 |
Move to new file position. Argument *offset* is a byte count. Optional |
| 127 |
argument *whence* defaults to ``os.SEEK_SET`` or ``0`` (offset from start |
| 128 |
of file; offset should be ``>= 0``); other values are ``os.SEEK_CUR`` or |
| 129 |
``1`` (move relative to current position; offset can be positive or |
| 130 |
negative), and ``os.SEEK_END`` or ``2`` (move relative to end of file; |
| 131 |
offset is usually negative, although many platforms allow seeking beyond |
| 132 |
the end of a file). |
| 133 |
|
| 134 |
Note that seeking of bz2 files is emulated, and depending on the |
| 135 |
parameters the operation may be extremely slow. |
| 136 |
|
| 137 |
|
| 138 |
.. method:: tell() |
| 139 |
|
| 140 |
Return the current file position, an integer (may be a long integer). |
| 141 |
|
| 142 |
|
| 143 |
.. method:: write(data) |
| 144 |
|
| 145 |
Write string *data* to file. Note that due to buffering, :meth:`close` may |
| 146 |
be needed before the file on disk reflects the data written. |
| 147 |
|
| 148 |
|
| 149 |
.. method:: writelines(sequence_of_strings) |
| 150 |
|
| 151 |
Write the sequence of strings to the file. Note that newlines are not |
| 152 |
added. The sequence can be any iterable object producing strings. This is |
| 153 |
equivalent to calling write() for each string. |
| 154 |
|
| 155 |
|
| 156 |
Sequential (de)compression |
| 157 |
-------------------------- |
| 158 |
|
| 159 |
Sequential compression and decompression is done using the classes |
| 160 |
:class:`BZ2Compressor` and :class:`BZ2Decompressor`. |
| 161 |
|
| 162 |
|
| 163 |
.. class:: BZ2Compressor([compresslevel]) |
| 164 |
|
| 165 |
Create a new compressor object. This object may be used to compress data |
| 166 |
sequentially. If you want to compress data in one shot, use the |
| 167 |
:func:`compress` function instead. The *compresslevel* parameter, if given, |
| 168 |
must be a number between ``1`` and ``9``; the default is ``9``. |
| 169 |
|
| 170 |
|
| 171 |
.. method:: compress(data) |
| 172 |
|
| 173 |
Provide more data to the compressor object. It will return chunks of |
| 174 |
compressed data whenever possible. When you've finished providing data to |
| 175 |
compress, call the :meth:`flush` method to finish the compression process, |
| 176 |
and return what is left in internal buffers. |
| 177 |
|
| 178 |
|
| 179 |
.. method:: flush() |
| 180 |
|
| 181 |
Finish the compression process and return what is left in internal |
| 182 |
buffers. You must not use the compressor object after calling this method. |
| 183 |
|
| 184 |
|
| 185 |
.. class:: BZ2Decompressor() |
| 186 |
|
| 187 |
Create a new decompressor object. This object may be used to decompress data |
| 188 |
sequentially. If you want to decompress data in one shot, use the |
| 189 |
:func:`decompress` function instead. |
| 190 |
|
| 191 |
|
| 192 |
.. method:: decompress(data) |
| 193 |
|
| 194 |
Provide more data to the decompressor object. It will return chunks of |
| 195 |
decompressed data whenever possible. If you try to decompress data after |
| 196 |
the end of stream is found, :exc:`EOFError` will be raised. If any data |
| 197 |
was found after the end of stream, it'll be ignored and saved in |
| 198 |
:attr:`unused_data` attribute. |
| 199 |
|
| 200 |
|
| 201 |
One-shot (de)compression |
| 202 |
------------------------ |
| 203 |
|
| 204 |
One-shot compression and decompression is provided through the :func:`compress` |
| 205 |
and :func:`decompress` functions. |
| 206 |
|
| 207 |
|
| 208 |
.. function:: compress(data[, compresslevel]) |
| 209 |
|
| 210 |
Compress *data* in one shot. If you want to compress data sequentially, use |
| 211 |
an instance of :class:`BZ2Compressor` instead. The *compresslevel* parameter, |
| 212 |
if given, must be a number between ``1`` and ``9``; the default is ``9``. |
| 213 |
|
| 214 |
|
| 215 |
.. function:: decompress(data) |
| 216 |
|
| 217 |
Decompress *data* in one shot. If you want to decompress data sequentially, |
| 218 |
use an instance of :class:`BZ2Decompressor` instead. |
| 219 |
|