blob: d9a2bad7567f857fb3e663d5d8e6a77462bc05e2 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`bz2` --- Compression compatible with :program:`bzip2`
2===========================================================
3
4.. module:: bz2
Georg Brandl0d8f0732009-04-05 22:20:44 +00005 :synopsis: Interface to compression and decompression routines
6 compatible with bzip2.
Georg Brandl116aa622007-08-15 14:28:22 +00007.. moduleauthor:: Gustavo Niemeyer <niemeyer@conectiva.com>
8.. sectionauthor:: Gustavo Niemeyer <niemeyer@conectiva.com>
9
10
Georg Brandl116aa622007-08-15 14:28:22 +000011This module provides a comprehensive interface for the bz2 compression library.
12It implements a complete file interface, one-shot (de)compression functions, and
13types for sequential (de)compression.
14
Guido van Rossum77677112007-11-05 19:43:04 +000015For other archive formats, see the :mod:`gzip`, :mod:`zipfile`, and
16:mod:`tarfile` modules.
17
18Here is a summary of the features offered by the bz2 module:
Georg Brandl116aa622007-08-15 14:28:22 +000019
20* :class:`BZ2File` class implements a complete file interface, including
Ezio Melotti9ebc1872010-03-13 00:27:48 +000021 :meth:`~BZ2File.readline`, :meth:`~BZ2File.readlines`,
22 :meth:`~BZ2File.writelines`, :meth:`~BZ2File.seek`, etc;
Georg Brandl116aa622007-08-15 14:28:22 +000023
Ezio Melotti9ebc1872010-03-13 00:27:48 +000024* :class:`BZ2File` class implements emulated :meth:`~BZ2File.seek` support;
Georg Brandl116aa622007-08-15 14:28:22 +000025
26* :class:`BZ2File` class implements universal newline support;
27
Antoine Pitrou25d535e2010-09-15 11:25:11 +000028* :class:`BZ2File` class offers an optimized line iteration using a readahead
29 algorithm;
Georg Brandl116aa622007-08-15 14:28:22 +000030
31* Sequential (de)compression supported by :class:`BZ2Compressor` and
32 :class:`BZ2Decompressor` classes;
33
34* One-shot (de)compression supported by :func:`compress` and :func:`decompress`
35 functions;
36
Guido van Rossum77677112007-11-05 19:43:04 +000037* Thread safety uses individual locking mechanism.
Georg Brandl116aa622007-08-15 14:28:22 +000038
39
40(De)compression of files
41------------------------
42
43Handling of compressed files is offered by the :class:`BZ2File` class.
44
45
Georg Brandl0d8f0732009-04-05 22:20:44 +000046.. class:: BZ2File(filename, mode='r', buffering=0, compresslevel=9)
Georg Brandl116aa622007-08-15 14:28:22 +000047
Benjamin Petersone41251e2008-04-25 01:59:09 +000048 Open a bz2 file. Mode can be either ``'r'`` or ``'w'``, for reading (default)
Georg Brandl116aa622007-08-15 14:28:22 +000049 or writing. When opened for writing, the file will be created if it doesn't
Benjamin Petersone41251e2008-04-25 01:59:09 +000050 exist, and truncated otherwise. If *buffering* is given, ``0`` means
51 unbuffered, and larger numbers specify the buffer size; the default is
52 ``0``. If *compresslevel* is given, it must be a number between ``1`` and
53 ``9``; the default is ``9``. Add a ``'U'`` to mode to open the file for input
54 with universal newline support. Any line ending in the input file will be
55 seen as a ``'\n'`` in Python. Also, a file so opened gains the attribute
Georg Brandl116aa622007-08-15 14:28:22 +000056 :attr:`newlines`; the value for this attribute is one of ``None`` (no newline
Benjamin Petersone41251e2008-04-25 01:59:09 +000057 read yet), ``'\r'``, ``'\n'``, ``'\r\n'`` or a tuple containing all the
58 newline types seen. Universal newlines are available only when
59 reading. Instances support iteration in the same way as normal :class:`file`
60 instances.
Georg Brandl116aa622007-08-15 14:28:22 +000061
Benjamin Petersone0124bd2009-03-09 21:04:33 +000062 :class:`BZ2File` supports the :keyword:`with` statement.
63
Benjamin Peterson10745a92009-03-09 21:08:47 +000064 .. versionchanged:: 3.1
Benjamin Petersone0124bd2009-03-09 21:04:33 +000065 Support for the :keyword:`with` statement was added.
66
Georg Brandl116aa622007-08-15 14:28:22 +000067
Benjamin Petersone41251e2008-04-25 01:59:09 +000068 .. method:: close()
Georg Brandl116aa622007-08-15 14:28:22 +000069
Benjamin Petersone41251e2008-04-25 01:59:09 +000070 Close the file. Sets data attribute :attr:`closed` to true. A closed file
71 cannot be used for further I/O operations. :meth:`close` may be called
72 more than once without error.
Georg Brandl116aa622007-08-15 14:28:22 +000073
74
Benjamin Petersone41251e2008-04-25 01:59:09 +000075 .. method:: read([size])
Georg Brandl116aa622007-08-15 14:28:22 +000076
Ezio Melottiede5d832010-03-12 22:48:16 +000077 Read at most *size* uncompressed bytes, returned as a byte string. If the
Benjamin Petersone41251e2008-04-25 01:59:09 +000078 *size* argument is negative or omitted, read until EOF is reached.
Georg Brandl116aa622007-08-15 14:28:22 +000079
80
Benjamin Petersone41251e2008-04-25 01:59:09 +000081 .. method:: readline([size])
Georg Brandl116aa622007-08-15 14:28:22 +000082
Ezio Melottiede5d832010-03-12 22:48:16 +000083 Return the next line from the file, as a byte string, retaining newline.
84 A non-negative *size* argument limits the maximum number of bytes to
85 return (an incomplete line may be returned then). Return an empty byte
86 string at EOF.
Georg Brandl116aa622007-08-15 14:28:22 +000087
88
Benjamin Petersone41251e2008-04-25 01:59:09 +000089 .. method:: readlines([size])
Georg Brandl116aa622007-08-15 14:28:22 +000090
Benjamin Petersone41251e2008-04-25 01:59:09 +000091 Return a list of lines read. The optional *size* argument, if given, is an
92 approximate bound on the total number of bytes in the lines returned.
Georg Brandl116aa622007-08-15 14:28:22 +000093
94
Benjamin Petersone41251e2008-04-25 01:59:09 +000095 .. method:: seek(offset[, whence])
Georg Brandl116aa622007-08-15 14:28:22 +000096
Benjamin Petersone41251e2008-04-25 01:59:09 +000097 Move to new file position. Argument *offset* is a byte count. Optional
98 argument *whence* defaults to ``os.SEEK_SET`` or ``0`` (offset from start
99 of file; offset should be ``>= 0``); other values are ``os.SEEK_CUR`` or
100 ``1`` (move relative to current position; offset can be positive or
101 negative), and ``os.SEEK_END`` or ``2`` (move relative to end of file;
102 offset is usually negative, although many platforms allow seeking beyond
103 the end of a file).
Georg Brandl116aa622007-08-15 14:28:22 +0000104
Benjamin Petersone41251e2008-04-25 01:59:09 +0000105 Note that seeking of bz2 files is emulated, and depending on the
106 parameters the operation may be extremely slow.
Georg Brandl116aa622007-08-15 14:28:22 +0000107
108
Benjamin Petersone41251e2008-04-25 01:59:09 +0000109 .. method:: tell()
Georg Brandl116aa622007-08-15 14:28:22 +0000110
Benjamin Petersone41251e2008-04-25 01:59:09 +0000111 Return the current file position, an integer.
Georg Brandl116aa622007-08-15 14:28:22 +0000112
113
Benjamin Petersone41251e2008-04-25 01:59:09 +0000114 .. method:: write(data)
Georg Brandl116aa622007-08-15 14:28:22 +0000115
Ezio Melottiede5d832010-03-12 22:48:16 +0000116 Write the byte string *data* to file. Note that due to buffering,
117 :meth:`close` may be needed before the file on disk reflects the data
118 written.
Georg Brandl116aa622007-08-15 14:28:22 +0000119
120
Ezio Melottiede5d832010-03-12 22:48:16 +0000121 .. method:: writelines(sequence_of_byte_strings)
Georg Brandl116aa622007-08-15 14:28:22 +0000122
Ezio Melottiede5d832010-03-12 22:48:16 +0000123 Write the sequence of byte strings to the file. Note that newlines are not
124 added. The sequence can be any iterable object producing byte strings.
125 This is equivalent to calling write() for each byte string.
Georg Brandl116aa622007-08-15 14:28:22 +0000126
127
128Sequential (de)compression
129--------------------------
130
131Sequential compression and decompression is done using the classes
132:class:`BZ2Compressor` and :class:`BZ2Decompressor`.
133
134
Georg Brandl0d8f0732009-04-05 22:20:44 +0000135.. class:: BZ2Compressor(compresslevel=9)
Georg Brandl116aa622007-08-15 14:28:22 +0000136
137 Create a new compressor object. This object may be used to compress data
Benjamin Petersone41251e2008-04-25 01:59:09 +0000138 sequentially. If you want to compress data in one shot, use the
139 :func:`compress` function instead. The *compresslevel* parameter, if given,
140 must be a number between ``1`` and ``9``; the default is ``9``.
Georg Brandl116aa622007-08-15 14:28:22 +0000141
Benjamin Petersone41251e2008-04-25 01:59:09 +0000142 .. method:: compress(data)
Georg Brandl116aa622007-08-15 14:28:22 +0000143
Benjamin Petersone41251e2008-04-25 01:59:09 +0000144 Provide more data to the compressor object. It will return chunks of
145 compressed data whenever possible. When you've finished providing data to
146 compress, call the :meth:`flush` method to finish the compression process,
147 and return what is left in internal buffers.
Georg Brandl116aa622007-08-15 14:28:22 +0000148
149
Benjamin Petersone41251e2008-04-25 01:59:09 +0000150 .. method:: flush()
Georg Brandl116aa622007-08-15 14:28:22 +0000151
Benjamin Petersone41251e2008-04-25 01:59:09 +0000152 Finish the compression process and return what is left in internal
153 buffers. You must not use the compressor object after calling this method.
Georg Brandl116aa622007-08-15 14:28:22 +0000154
155
156.. class:: BZ2Decompressor()
157
158 Create a new decompressor object. This object may be used to decompress data
159 sequentially. If you want to decompress data in one shot, use the
160 :func:`decompress` function instead.
161
Benjamin Petersone41251e2008-04-25 01:59:09 +0000162 .. method:: decompress(data)
Georg Brandl116aa622007-08-15 14:28:22 +0000163
Benjamin Petersone41251e2008-04-25 01:59:09 +0000164 Provide more data to the decompressor object. It will return chunks of
165 decompressed data whenever possible. If you try to decompress data after
166 the end of stream is found, :exc:`EOFError` will be raised. If any data
167 was found after the end of stream, it'll be ignored and saved in
168 :attr:`unused_data` attribute.
Georg Brandl116aa622007-08-15 14:28:22 +0000169
170
171One-shot (de)compression
172------------------------
173
174One-shot compression and decompression is provided through the :func:`compress`
175and :func:`decompress` functions.
176
177
Georg Brandl0d8f0732009-04-05 22:20:44 +0000178.. function:: compress(data, compresslevel=9)
Georg Brandl116aa622007-08-15 14:28:22 +0000179
Benjamin Petersone41251e2008-04-25 01:59:09 +0000180 Compress *data* in one shot. If you want to compress data sequentially, use
181 an instance of :class:`BZ2Compressor` instead. The *compresslevel* parameter,
182 if given, must be a number between ``1`` and ``9``; the default is ``9``.
Georg Brandl116aa622007-08-15 14:28:22 +0000183
184
185.. function:: decompress(data)
186
Benjamin Petersone41251e2008-04-25 01:59:09 +0000187 Decompress *data* in one shot. If you want to decompress data sequentially,
188 use an instance of :class:`BZ2Decompressor` instead.
Georg Brandl116aa622007-08-15 14:28:22 +0000189