Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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 <niemeyer@conectiva.com> |
| 8 | .. sectionauthor:: Gustavo Niemeyer <niemeyer@conectiva.com> |
| 9 | |
| 10 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 11 | This module provides a comprehensive interface for the bz2 compression library. |
| 12 | It implements a complete file interface, one-shot (de)compression functions, and |
| 13 | types for sequential (de)compression. |
| 14 | |
Guido van Rossum | 7767711 | 2007-11-05 19:43:04 +0000 | [diff] [blame] | 15 | For other archive formats, see the :mod:`gzip`, :mod:`zipfile`, and |
| 16 | :mod:`tarfile` modules. |
| 17 | |
| 18 | Here is a summary of the features offered by the bz2 module: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 19 | |
| 20 | * :class:`BZ2File` class implements a complete file interface, including |
| 21 | :meth:`readline`, :meth:`readlines`, :meth:`writelines`, :meth:`seek`, etc; |
| 22 | |
| 23 | * :class:`BZ2File` class implements emulated :meth:`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 | |
Guido van Rossum | 7767711 | 2007-11-05 19:43:04 +0000 | [diff] [blame] | 36 | * Thread safety uses individual locking mechanism. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 37 | |
| 38 | |
| 39 | (De)compression of files |
| 40 | ------------------------ |
| 41 | |
| 42 | Handling of compressed files is offered by the :class:`BZ2File` class. |
| 43 | |
| 44 | |
| 45 | .. class:: BZ2File(filename[, mode[, buffering[, compresslevel]]]) |
| 46 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 47 | Open a bz2 file. Mode can be either ``'r'`` or ``'w'``, for reading (default) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 48 | or writing. When opened for writing, the file will be created if it doesn't |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 49 | exist, and truncated otherwise. If *buffering* is given, ``0`` means |
| 50 | unbuffered, and larger numbers specify the buffer size; the default is |
| 51 | ``0``. If *compresslevel* is given, it must be a number between ``1`` and |
| 52 | ``9``; the default is ``9``. Add a ``'U'`` to mode to open the file for input |
| 53 | with universal newline support. Any line ending in the input file will be |
| 54 | seen as a ``'\n'`` in Python. Also, a file so opened gains the attribute |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 55 | :attr:`newlines`; the value for this attribute is one of ``None`` (no newline |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 56 | read yet), ``'\r'``, ``'\n'``, ``'\r\n'`` or a tuple containing all the |
| 57 | newline types seen. Universal newlines are available only when |
| 58 | reading. Instances support iteration in the same way as normal :class:`file` |
| 59 | instances. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 60 | |
Benjamin Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 61 | :class:`BZ2File` supports the :keyword:`with` statement. |
| 62 | |
Benjamin Peterson | 10745a9 | 2009-03-09 21:08:47 +0000 | [diff] [blame] | 63 | .. versionchanged:: 3.1 |
Benjamin Peterson | e0124bd | 2009-03-09 21:04:33 +0000 | [diff] [blame] | 64 | Support for the :keyword:`with` statement was added. |
| 65 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 66 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 67 | .. method:: close() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 68 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 69 | Close the file. Sets data attribute :attr:`closed` to true. A closed file |
| 70 | cannot be used for further I/O operations. :meth:`close` may be called |
| 71 | more than once without error. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 72 | |
| 73 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 74 | .. method:: read([size]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 75 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 76 | Read at most *size* uncompressed bytes, returned as a string. If the |
| 77 | *size* argument is negative or omitted, read until EOF is reached. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 78 | |
| 79 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 80 | .. method:: readline([size]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 81 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 82 | Return the next line from the file, as a string, retaining newline. A |
| 83 | non-negative *size* argument limits the maximum number of bytes to return |
| 84 | (an incomplete line may be returned then). Return an empty string at EOF. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 85 | |
| 86 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 87 | .. method:: readlines([size]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 88 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 89 | Return a list of lines read. The optional *size* argument, if given, is an |
| 90 | approximate bound on the total number of bytes in the lines returned. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 91 | |
| 92 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 93 | .. method:: seek(offset[, whence]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 94 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 95 | Move to new file position. Argument *offset* is a byte count. Optional |
| 96 | argument *whence* defaults to ``os.SEEK_SET`` or ``0`` (offset from start |
| 97 | of file; offset should be ``>= 0``); other values are ``os.SEEK_CUR`` or |
| 98 | ``1`` (move relative to current position; offset can be positive or |
| 99 | negative), and ``os.SEEK_END`` or ``2`` (move relative to end of file; |
| 100 | offset is usually negative, although many platforms allow seeking beyond |
| 101 | the end of a file). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 102 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 103 | Note that seeking of bz2 files is emulated, and depending on the |
| 104 | parameters the operation may be extremely slow. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 105 | |
| 106 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 107 | .. method:: tell() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 108 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 109 | Return the current file position, an integer. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 110 | |
| 111 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 112 | .. method:: write(data) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 113 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 114 | Write string *data* to file. Note that due to buffering, :meth:`close` may |
| 115 | be needed before the file on disk reflects the data written. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 116 | |
| 117 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 118 | .. method:: writelines(sequence_of_strings) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 119 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 120 | Write the sequence of strings to the file. Note that newlines are not |
| 121 | added. The sequence can be any iterable object producing strings. This is |
| 122 | equivalent to calling write() for each string. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 123 | |
| 124 | |
| 125 | Sequential (de)compression |
| 126 | -------------------------- |
| 127 | |
| 128 | Sequential compression and decompression is done using the classes |
| 129 | :class:`BZ2Compressor` and :class:`BZ2Decompressor`. |
| 130 | |
| 131 | |
| 132 | .. class:: BZ2Compressor([compresslevel]) |
| 133 | |
| 134 | Create a new compressor object. This object may be used to compress data |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 135 | sequentially. If you want to compress data in one shot, use the |
| 136 | :func:`compress` function instead. The *compresslevel* parameter, if given, |
| 137 | must be a number between ``1`` and ``9``; the default is ``9``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 138 | |
| 139 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 140 | .. method:: compress(data) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 141 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 142 | Provide more data to the compressor object. It will return chunks of |
| 143 | compressed data whenever possible. When you've finished providing data to |
| 144 | compress, call the :meth:`flush` method to finish the compression process, |
| 145 | and return what is left in internal buffers. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 146 | |
| 147 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 148 | .. method:: flush() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 149 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 150 | Finish the compression process and return what is left in internal |
| 151 | buffers. You must not use the compressor object after calling this method. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 152 | |
| 153 | |
| 154 | .. class:: BZ2Decompressor() |
| 155 | |
| 156 | Create a new decompressor object. This object may be used to decompress data |
| 157 | sequentially. If you want to decompress data in one shot, use the |
| 158 | :func:`decompress` function instead. |
| 159 | |
| 160 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 161 | .. method:: decompress(data) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 162 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 163 | Provide more data to the decompressor object. It will return chunks of |
| 164 | decompressed data whenever possible. If you try to decompress data after |
| 165 | the end of stream is found, :exc:`EOFError` will be raised. If any data |
| 166 | was found after the end of stream, it'll be ignored and saved in |
| 167 | :attr:`unused_data` attribute. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 168 | |
| 169 | |
| 170 | One-shot (de)compression |
| 171 | ------------------------ |
| 172 | |
| 173 | One-shot compression and decompression is provided through the :func:`compress` |
| 174 | and :func:`decompress` functions. |
| 175 | |
| 176 | |
| 177 | .. function:: compress(data[, compresslevel]) |
| 178 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 179 | Compress *data* in one shot. If you want to compress data sequentially, use |
| 180 | an instance of :class:`BZ2Compressor` instead. The *compresslevel* parameter, |
| 181 | if given, must be a number between ``1`` and ``9``; the default is ``9``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 182 | |
| 183 | |
| 184 | .. function:: decompress(data) |
| 185 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 186 | Decompress *data* in one shot. If you want to decompress data sequentially, |
| 187 | use an instance of :class:`BZ2Decompressor` instead. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 188 | |