<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>13.3. bz2 — Support for bzip2 compression — Python v3.3.2 documentation</title> <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '3.3.2', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python v3.3.2 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python v3.3.2 documentation" href="../index.html" /> <link rel="up" title="13. Data Compression and Archiving" href="archiving.html" /> <link rel="next" title="13.4. lzma — Compression using the LZMA algorithm" href="lzma.html" /> <link rel="prev" title="13.2. gzip — Support for gzip files" href="gzip.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="lzma.html" title="13.4. lzma — Compression using the LZMA algorithm" accesskey="N">next</a> |</li> <li class="right" > <a href="gzip.html" title="13.2. gzip — Support for gzip files" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">3.3.2 Documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="archiving.html" accesskey="U">13. Data Compression and Archiving</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="module-bz2"> <span id="bz2-support-for-bzip2-compression"></span><h1>13.3. <a class="reference internal" href="#module-bz2" title="bz2: Interfaces for bzip2 compression and decompression."><tt class="xref py py-mod docutils literal"><span class="pre">bz2</span></tt></a> — Support for <strong class="program">bzip2</strong> compression<a class="headerlink" href="#module-bz2" title="Permalink to this headline">¶</a></h1> <p>This module provides a comprehensive interface for compressing and decompressing data using the bzip2 compression algorithm.</p> <p>The <a class="reference internal" href="#module-bz2" title="bz2: Interfaces for bzip2 compression and decompression."><tt class="xref py py-mod docutils literal"><span class="pre">bz2</span></tt></a> module contains:</p> <ul class="simple"> <li>The <a class="reference internal" href="#bz2.open" title="bz2.open"><tt class="xref py py-func docutils literal"><span class="pre">open()</span></tt></a> function and <a class="reference internal" href="#bz2.BZ2File" title="bz2.BZ2File"><tt class="xref py py-class docutils literal"><span class="pre">BZ2File</span></tt></a> class for reading and writing compressed files.</li> <li>The <a class="reference internal" href="#bz2.BZ2Compressor" title="bz2.BZ2Compressor"><tt class="xref py py-class docutils literal"><span class="pre">BZ2Compressor</span></tt></a> and <a class="reference internal" href="#bz2.BZ2Decompressor" title="bz2.BZ2Decompressor"><tt class="xref py py-class docutils literal"><span class="pre">BZ2Decompressor</span></tt></a> classes for incremental (de)compression.</li> <li>The <a class="reference internal" href="#bz2.compress" title="bz2.compress"><tt class="xref py py-func docutils literal"><span class="pre">compress()</span></tt></a> and <a class="reference internal" href="#bz2.decompress" title="bz2.decompress"><tt class="xref py py-func docutils literal"><span class="pre">decompress()</span></tt></a> functions for one-shot (de)compression.</li> </ul> <p>All of the classes in this module may safely be accessed from multiple threads.</p> <div class="section" id="de-compression-of-files"> <h2>13.3.1. (De)compression of files<a class="headerlink" href="#de-compression-of-files" title="Permalink to this headline">¶</a></h2> <dl class="function"> <dt id="bz2.open"> <tt class="descclassname">bz2.</tt><tt class="descname">open</tt><big>(</big><em>filename</em>, <em>mode='r'</em>, <em>compresslevel=9</em>, <em>encoding=None</em>, <em>errors=None</em>, <em>newline=None</em><big>)</big><a class="headerlink" href="#bz2.open" title="Permalink to this definition">¶</a></dt> <dd><p>Open a bzip2-compressed file in binary or text mode, returning a <a class="reference internal" href="../glossary.html#term-file-object"><em class="xref std std-term">file object</em></a>.</p> <p>As with the constructor for <a class="reference internal" href="#bz2.BZ2File" title="bz2.BZ2File"><tt class="xref py py-class docutils literal"><span class="pre">BZ2File</span></tt></a>, the <em>filename</em> argument can be an actual filename (a <a class="reference internal" href="stdtypes.html#str" title="str"><tt class="xref py py-class docutils literal"><span class="pre">str</span></tt></a> or <a class="reference internal" href="functions.html#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></a> object), or an existing file object to read from or write to.</p> <p>The <em>mode</em> argument can be any of <tt class="docutils literal"><span class="pre">'r'</span></tt>, <tt class="docutils literal"><span class="pre">'rb'</span></tt>, <tt class="docutils literal"><span class="pre">'w'</span></tt>, <tt class="docutils literal"><span class="pre">'wb'</span></tt>, <tt class="docutils literal"><span class="pre">'a'</span></tt>, or <tt class="docutils literal"><span class="pre">'ab'</span></tt> for binary mode, or <tt class="docutils literal"><span class="pre">'rt'</span></tt>, <tt class="docutils literal"><span class="pre">'wt'</span></tt>, or <tt class="docutils literal"><span class="pre">'at'</span></tt> for text mode. The default is <tt class="docutils literal"><span class="pre">'rb'</span></tt>.</p> <p>The <em>compresslevel</em> argument is an integer from 1 to 9, as for the <a class="reference internal" href="#bz2.BZ2File" title="bz2.BZ2File"><tt class="xref py py-class docutils literal"><span class="pre">BZ2File</span></tt></a> constructor.</p> <p>For binary mode, this function is equivalent to the <a class="reference internal" href="#bz2.BZ2File" title="bz2.BZ2File"><tt class="xref py py-class docutils literal"><span class="pre">BZ2File</span></tt></a> constructor: <tt class="docutils literal"><span class="pre">BZ2File(filename,</span> <span class="pre">mode,</span> <span class="pre">compresslevel=compresslevel)</span></tt>. In this case, the <em>encoding</em>, <em>errors</em> and <em>newline</em> arguments must not be provided.</p> <p>For text mode, a <a class="reference internal" href="#bz2.BZ2File" title="bz2.BZ2File"><tt class="xref py py-class docutils literal"><span class="pre">BZ2File</span></tt></a> object is created, and wrapped in an <a class="reference internal" href="io.html#io.TextIOWrapper" title="io.TextIOWrapper"><tt class="xref py py-class docutils literal"><span class="pre">io.TextIOWrapper</span></tt></a> instance with the specified encoding, error handling behavior, and line ending(s).</p> <p class="versionadded"> <span class="versionmodified">New in version 3.3.</span> </p> </dd></dl> <dl class="class"> <dt id="bz2.BZ2File"> <em class="property">class </em><tt class="descclassname">bz2.</tt><tt class="descname">BZ2File</tt><big>(</big><em>filename</em>, <em>mode='r'</em>, <em>buffering=None</em>, <em>compresslevel=9</em><big>)</big><a class="headerlink" href="#bz2.BZ2File" title="Permalink to this definition">¶</a></dt> <dd><p>Open a bzip2-compressed file in binary mode.</p> <p>If <em>filename</em> is a <a class="reference internal" href="stdtypes.html#str" title="str"><tt class="xref py py-class docutils literal"><span class="pre">str</span></tt></a> or <a class="reference internal" href="functions.html#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></a> object, open the named file directly. Otherwise, <em>filename</em> should be a <a class="reference internal" href="../glossary.html#term-file-object"><em class="xref std std-term">file object</em></a>, which will be used to read or write the compressed data.</p> <p>The <em>mode</em> argument can be either <tt class="docutils literal"><span class="pre">'r'</span></tt> for reading (default), <tt class="docutils literal"><span class="pre">'w'</span></tt> for overwriting, or <tt class="docutils literal"><span class="pre">'a'</span></tt> for appending. These can equivalently be given as <tt class="docutils literal"><span class="pre">'rb'</span></tt>, <tt class="docutils literal"><span class="pre">'wb'</span></tt>, and <tt class="docutils literal"><span class="pre">'ab'</span></tt> respectively.</p> <p>If <em>filename</em> is a file object (rather than an actual file name), a mode of <tt class="docutils literal"><span class="pre">'w'</span></tt> does not truncate the file, and is instead equivalent to <tt class="docutils literal"><span class="pre">'a'</span></tt>.</p> <p>The <em>buffering</em> argument is ignored. Its use is deprecated.</p> <p>If <em>mode</em> is <tt class="docutils literal"><span class="pre">'w'</span></tt> or <tt class="docutils literal"><span class="pre">'a'</span></tt>, <em>compresslevel</em> can be a number between <tt class="docutils literal"><span class="pre">1</span></tt> and <tt class="docutils literal"><span class="pre">9</span></tt> specifying the level of compression: <tt class="docutils literal"><span class="pre">1</span></tt> produces the least compression, and <tt class="docutils literal"><span class="pre">9</span></tt> (default) produces the most compression.</p> <p>If <em>mode</em> is <tt class="docutils literal"><span class="pre">'r'</span></tt>, the input file may be the concatenation of multiple compressed streams.</p> <p><a class="reference internal" href="#bz2.BZ2File" title="bz2.BZ2File"><tt class="xref py py-class docutils literal"><span class="pre">BZ2File</span></tt></a> provides all of the members specified by the <a class="reference internal" href="io.html#io.BufferedIOBase" title="io.BufferedIOBase"><tt class="xref py py-class docutils literal"><span class="pre">io.BufferedIOBase</span></tt></a>, except for <tt class="xref py py-meth docutils literal"><span class="pre">detach()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">truncate()</span></tt>. Iteration and the <a class="reference internal" href="../reference/compound_stmts.html#with"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></a> statement are supported.</p> <p><a class="reference internal" href="#bz2.BZ2File" title="bz2.BZ2File"><tt class="xref py py-class docutils literal"><span class="pre">BZ2File</span></tt></a> also provides the following method:</p> <dl class="method"> <dt id="bz2.BZ2File.peek"> <tt class="descname">peek</tt><big>(</big><span class="optional">[</span><em>n</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#bz2.BZ2File.peek" title="Permalink to this definition">¶</a></dt> <dd><p>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.</p> <p class="versionadded"> <span class="versionmodified">New in version 3.3.</span> </p> </dd></dl> <p class="versionchanged"> <span class="versionmodified">Changed in version 3.1:</span> Support for the <a class="reference internal" href="../reference/compound_stmts.html#with"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></a> statement was added.</p> <p class="versionchanged"> <span class="versionmodified">Changed in version 3.3:</span> The <tt class="xref py py-meth docutils literal"><span class="pre">fileno()</span></tt>, <tt class="xref py py-meth docutils literal"><span class="pre">readable()</span></tt>, <tt class="xref py py-meth docutils literal"><span class="pre">seekable()</span></tt>, <tt class="xref py py-meth docutils literal"><span class="pre">writable()</span></tt>, <tt class="xref py py-meth docutils literal"><span class="pre">read1()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">readinto()</span></tt> methods were added.</p> <p class="versionchanged"> <span class="versionmodified">Changed in version 3.3:</span> Support was added for <em>filename</em> being a <a class="reference internal" href="../glossary.html#term-file-object"><em class="xref std std-term">file object</em></a> instead of an actual filename.</p> <p class="versionchanged"> <span class="versionmodified">Changed in version 3.3:</span> The <tt class="docutils literal"><span class="pre">'a'</span></tt> (append) mode was added, along with support for reading multi-stream files.</p> </dd></dl> </div> <div class="section" id="incremental-de-compression"> <h2>13.3.2. Incremental (de)compression<a class="headerlink" href="#incremental-de-compression" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt id="bz2.BZ2Compressor"> <em class="property">class </em><tt class="descclassname">bz2.</tt><tt class="descname">BZ2Compressor</tt><big>(</big><em>compresslevel=9</em><big>)</big><a class="headerlink" href="#bz2.BZ2Compressor" title="Permalink to this definition">¶</a></dt> <dd><p>Create a new compressor object. This object may be used to compress data incrementally. For one-shot compression, use the <a class="reference internal" href="#bz2.compress" title="bz2.compress"><tt class="xref py py-func docutils literal"><span class="pre">compress()</span></tt></a> function instead.</p> <p><em>compresslevel</em>, if given, must be a number between <tt class="docutils literal"><span class="pre">1</span></tt> and <tt class="docutils literal"><span class="pre">9</span></tt>. The default is <tt class="docutils literal"><span class="pre">9</span></tt>.</p> <dl class="method"> <dt id="bz2.BZ2Compressor.compress"> <tt class="descname">compress</tt><big>(</big><em>data</em><big>)</big><a class="headerlink" href="#bz2.BZ2Compressor.compress" title="Permalink to this definition">¶</a></dt> <dd><p>Provide data to the compressor object. Returns a chunk of compressed data if possible, or an empty byte string otherwise.</p> <p>When you have finished providing data to the compressor, call the <a class="reference internal" href="#bz2.BZ2Compressor.flush" title="bz2.BZ2Compressor.flush"><tt class="xref py py-meth docutils literal"><span class="pre">flush()</span></tt></a> method to finish the compression process.</p> </dd></dl> <dl class="method"> <dt id="bz2.BZ2Compressor.flush"> <tt class="descname">flush</tt><big>(</big><big>)</big><a class="headerlink" href="#bz2.BZ2Compressor.flush" title="Permalink to this definition">¶</a></dt> <dd><p>Finish the compression process. Returns the compressed data left in internal buffers.</p> <p>The compressor object may not be used after this method has been called.</p> </dd></dl> </dd></dl> <dl class="class"> <dt id="bz2.BZ2Decompressor"> <em class="property">class </em><tt class="descclassname">bz2.</tt><tt class="descname">BZ2Decompressor</tt><a class="headerlink" href="#bz2.BZ2Decompressor" title="Permalink to this definition">¶</a></dt> <dd><p>Create a new decompressor object. This object may be used to decompress data incrementally. For one-shot compression, use the <a class="reference internal" href="#bz2.decompress" title="bz2.decompress"><tt class="xref py py-func docutils literal"><span class="pre">decompress()</span></tt></a> function instead.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">This class does not transparently handle inputs containing multiple compressed streams, unlike <a class="reference internal" href="#bz2.decompress" title="bz2.decompress"><tt class="xref py py-func docutils literal"><span class="pre">decompress()</span></tt></a> and <a class="reference internal" href="#bz2.BZ2File" title="bz2.BZ2File"><tt class="xref py py-class docutils literal"><span class="pre">BZ2File</span></tt></a>. If you need to decompress a multi-stream input with <a class="reference internal" href="#bz2.BZ2Decompressor" title="bz2.BZ2Decompressor"><tt class="xref py py-class docutils literal"><span class="pre">BZ2Decompressor</span></tt></a>, you must use a new decompressor for each stream.</p> </div> <dl class="method"> <dt id="bz2.BZ2Decompressor.decompress"> <tt class="descname">decompress</tt><big>(</big><em>data</em><big>)</big><a class="headerlink" href="#bz2.BZ2Decompressor.decompress" title="Permalink to this definition">¶</a></dt> <dd><p>Provide data to the decompressor object. Returns a chunk of decompressed data if possible, or an empty byte string otherwise.</p> <p>Attempting to decompress data after the end of the current stream is reached raises an <a class="reference internal" href="exceptions.html#EOFError" title="EOFError"><tt class="xref py py-exc docutils literal"><span class="pre">EOFError</span></tt></a>. If any data is found after the end of the stream, it is ignored and saved in the <a class="reference internal" href="#bz2.BZ2Decompressor.unused_data" title="bz2.BZ2Decompressor.unused_data"><tt class="xref py py-attr docutils literal"><span class="pre">unused_data</span></tt></a> attribute.</p> </dd></dl> <dl class="attribute"> <dt id="bz2.BZ2Decompressor.eof"> <tt class="descname">eof</tt><a class="headerlink" href="#bz2.BZ2Decompressor.eof" title="Permalink to this definition">¶</a></dt> <dd><p>True if the end-of-stream marker has been reached.</p> <p class="versionadded"> <span class="versionmodified">New in version 3.3.</span> </p> </dd></dl> <dl class="attribute"> <dt id="bz2.BZ2Decompressor.unused_data"> <tt class="descname">unused_data</tt><a class="headerlink" href="#bz2.BZ2Decompressor.unused_data" title="Permalink to this definition">¶</a></dt> <dd><p>Data found after the end of the compressed stream.</p> <p>If this attribute is accessed before the end of the stream has been reached, its value will be <tt class="docutils literal"><span class="pre">b''</span></tt>.</p> </dd></dl> </dd></dl> </div> <div class="section" id="one-shot-de-compression"> <h2>13.3.3. One-shot (de)compression<a class="headerlink" href="#one-shot-de-compression" title="Permalink to this headline">¶</a></h2> <dl class="function"> <dt id="bz2.compress"> <tt class="descclassname">bz2.</tt><tt class="descname">compress</tt><big>(</big><em>data</em>, <em>compresslevel=9</em><big>)</big><a class="headerlink" href="#bz2.compress" title="Permalink to this definition">¶</a></dt> <dd><p>Compress <em>data</em>.</p> <p><em>compresslevel</em>, if given, must be a number between <tt class="docutils literal"><span class="pre">1</span></tt> and <tt class="docutils literal"><span class="pre">9</span></tt>. The default is <tt class="docutils literal"><span class="pre">9</span></tt>.</p> <p>For incremental compression, use a <a class="reference internal" href="#bz2.BZ2Compressor" title="bz2.BZ2Compressor"><tt class="xref py py-class docutils literal"><span class="pre">BZ2Compressor</span></tt></a> instead.</p> </dd></dl> <dl class="function"> <dt id="bz2.decompress"> <tt class="descclassname">bz2.</tt><tt class="descname">decompress</tt><big>(</big><em>data</em><big>)</big><a class="headerlink" href="#bz2.decompress" title="Permalink to this definition">¶</a></dt> <dd><p>Decompress <em>data</em>.</p> <p>If <em>data</em> is the concatenation of multiple compressed streams, decompress all of the streams.</p> <p>For incremental decompression, use a <a class="reference internal" href="#bz2.BZ2Decompressor" title="bz2.BZ2Decompressor"><tt class="xref py py-class docutils literal"><span class="pre">BZ2Decompressor</span></tt></a> instead.</p> <p class="versionchanged"> <span class="versionmodified">Changed in version 3.3:</span> Support for multi-stream inputs was added.</p> </dd></dl> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">13.3. <tt class="docutils literal"><span class="pre">bz2</span></tt> — Support for <strong class="program">bzip2</strong> compression</a><ul> <li><a class="reference internal" href="#de-compression-of-files">13.3.1. (De)compression of files</a></li> <li><a class="reference internal" href="#incremental-de-compression">13.3.2. Incremental (de)compression</a></li> <li><a class="reference internal" href="#one-shot-de-compression">13.3.3. One-shot (de)compression</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="gzip.html" title="previous chapter">13.2. <tt class="docutils literal"><span class="pre">gzip</span></tt> — Support for <strong class="program">gzip</strong> files</a></p> <h4>Next topic</h4> <p class="topless"><a href="lzma.html" title="next chapter">13.4. <tt class="docutils literal"><span class="pre">lzma</span></tt> — Compression using the LZMA algorithm</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li><a href="../_sources/library/bz2.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" size="18" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="lzma.html" title="13.4. lzma — Compression using the LZMA algorithm" >next</a> |</li> <li class="right" > <a href="gzip.html" title="13.2. gzip — Support for gzip files" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">3.3.2 Documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="archiving.html" >13. Data Compression and Archiving</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2013, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on May 15, 2013. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7. </div> </body> </html>