parthtamu/rag-code-assistant
0
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4 <head>5 <meta charset="utf-8" />6 <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="bz2 — Support for bzip2 compression" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/bz2.html" />10<meta property="og:site_name" content="Python documentation" />11<meta property="og:description" content="Source code: Lib/bz2.py This module provides a comprehensive interface for compressing and decompressing data using the bzip2 compression algorithm. The bz2 module contains: The open() function and..." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_bz2_8f1a2f36.png" />15<meta property="og:image:alt" content="Source code: Lib/bz2.py This module provides a comprehensive interface for compressing and decompressing data using the bzip2 compression algorithm. The bz2 module contains: The open() function and..." />16<meta name="description" content="Source code: Lib/bz2.py This module provides a comprehensive interface for compressing and decompressing data using the bzip2 compression algorithm. The bz2 module contains: The open() function and..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20 <title>bz2 — Support for bzip2 compression — Python 3.15.0a6 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">21 22 <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />23 <link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />24 <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />25 <link rel="stylesheet" type="text/css" href="../_static/profiling-sampling-visualization.css?v=0c2600ae" />26 <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />27 28 <script src="../_static/documentation_options.js?v=6b7c9ff5"></script>29 <script src="../_static/doctools.js?v=9bcbadda"></script>30 <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>31 <script src="../_static/profiling-sampling-visualization.js?v=9811ed04"></script>32 33 <script src="../_static/sidebar.js"></script>34 35 <link rel="search" type="application/opensearchdescription+xml"36 title="Search within Python 3.15.0a6 documentation"37 href="../_static/opensearch.xml"/>38 <link rel="author" title="About these documents" href="../about.html" />39 <link rel="index" title="Index" href="../genindex.html" />40 <link rel="search" title="Search" href="../search.html" />41 <link rel="copyright" title="Copyright" href="../copyright.html" />42 <link rel="next" title="lzma — Compression using the LZMA algorithm" href="lzma.html" />43 <link rel="prev" title="gzip — Support for gzip files" href="gzip.html" />44 45 46 <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47 48 <link rel="canonical" href="https://docs.python.org/3/library/bz2.html">49 50 51 52 53 <style>54 @media only screen {55 table.full-width-table {56 width: 100%;57 }58 }59 </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61 <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62 <script type="text/javascript" src="../_static/copybutton.js"></script>63 <script type="text/javascript" src="../_static/menu.js"></script>64 <script type="text/javascript" src="../_static/search-focus.js"></script>65 <script type="text/javascript" src="../_static/themetoggle.js"></script> 66 <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67 <meta name="readthedocs-addons-api-version" content="1">68 69 </head>70<body>71<div class="mobile-nav">72 <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73 aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74 <nav class="nav-content" role="navigation">75 <label for="menuToggler" class="toggler__label">76 <span></span>77 </label>78 <span class="nav-items-wrapper">79 <a href="https://www.python.org/" class="nav-logo">80 <img src="../_static/py.svg" alt="Python logo">81 </a>82 <span class="version_switcher_placeholder"></span>83 <form role="search" class="search" action="../search.html" method="get">84 <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85 <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>86 </svg>87 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88 <input type="submit" value="Go">89 </form>90 </span>91 </nav>92 <div class="menu-wrapper">93 <nav class="menu" role="navigation" aria-label="main navigation">94 <div class="language_switcher_placeholder"></div>95 96<label class="theme-selector-label">97 Theme98 <select class="theme-selector" oninput="activateTheme(this.value)">99 <option value="auto" selected>Auto</option>100 <option value="light">Light</option>101 <option value="dark">Dark</option>102 </select>103</label>104 <div>105 <h3><a href="../contents.html">Table of Contents</a></h3>106 <ul>107<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">bz2</span></code> — Support for <strong class="program">bzip2</strong> compression</a><ul>108<li><a class="reference internal" href="#de-compression-of-files">(De)compression of files</a></li>109<li><a class="reference internal" href="#incremental-de-compression">Incremental (de)compression</a></li>110<li><a class="reference internal" href="#one-shot-de-compression">One-shot (de)compression</a></li>111<li><a class="reference internal" href="#examples-of-usage">Examples of usage</a></li>112</ul>113</li>114</ul>115 116 </div>117 <div>118 <h4>Previous topic</h4>119 <p class="topless"><a href="gzip.html"120 title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gzip</span></code> — Support for <strong class="program">gzip</strong> files</a></p>121 </div>122 <div>123 <h4>Next topic</h4>124 <p class="topless"><a href="lzma.html"125 title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">lzma</span></code> — Compression using the LZMA algorithm</a></p>126 </div>127 <script>128 document.addEventListener('DOMContentLoaded', () => {129 const title = document.querySelector('meta[property="og:title"]').content;130 const elements = document.querySelectorAll('.improvepage');131 const pageurl = window.location.href.split('?')[0];132 elements.forEach(element => {133 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));134 url.searchParams.set('pagetitle', title);135 url.searchParams.set('pageurl', pageurl);136 url.searchParams.set('pagesource', "library/bz2.rst");137 element.href = url.toString();138 });139 });140 </script>141 <div role="note" aria-label="source link">142 <h3>This page</h3>143 <ul class="this-page-menu">144 <li><a href="../bugs.html">Report a bug</a></li>145 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>146 <li>147 <a href="https://github.com/python/cpython/blob/main/Doc/library/bz2.rst?plain=1"148 rel="nofollow">Show source149 </a>150 </li>151 152 </ul>153 </div>154 </nav>155 </div>156</div>157 158 159 <div class="related" role="navigation" aria-label="Related">160 <h3>Navigation</h3>161 <ul>162 <li class="right" style="margin-right: 10px">163 <a href="../genindex.html" title="General Index"164 accesskey="I">index</a></li>165 <li class="right" >166 <a href="../py-modindex.html" title="Python Module Index"167 >modules</a> |</li>168 <li class="right" >169 <a href="lzma.html" title="lzma — Compression using the LZMA algorithm"170 accesskey="N">next</a> |</li>171 <li class="right" >172 <a href="gzip.html" title="gzip — Support for gzip files"173 accesskey="P">previous</a> |</li>174 175 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>176 <li><a href="https://www.python.org/">Python</a> »</li>177 <li class="switchers">178 <div class="language_switcher_placeholder"></div>179 <div class="version_switcher_placeholder"></div>180 </li>181 <li>182 183 </li>184 <li id="cpython-language-and-version">185 <a href="../index.html">3.15.0a6 Documentation</a> »186 </li>187 188 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>189 <li class="nav-item nav-item-2"><a href="archiving.html" accesskey="U">Data Compression and Archiving</a> »</li>190 <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">bz2</span></code> — Support for <strong class="program">bzip2</strong> compression</a></li>191 <li class="right">192 193 194 <div class="inline-search" role="search">195 <form class="inline-search" action="../search.html" method="get">196 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">197 <input type="submit" value="Go">198 </form>199 </div>200 |201 </li>202 <li class="right">203<label class="theme-selector-label">204 Theme205 <select class="theme-selector" oninput="activateTheme(this.value)">206 <option value="auto" selected>Auto</option>207 <option value="light">Light</option>208 <option value="dark">Dark</option>209 </select>210</label> |</li>211 212 </ul>213 </div> 214 215 <div class="document">216 <div class="documentwrapper">217 <div class="bodywrapper">218 <div class="body" role="main">219 220 <section id="module-bz2">221<span id="bz2-support-for-bzip2-compression"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">bz2</span></code> — Support for <strong class="program">bzip2</strong> compression<a class="headerlink" href="#module-bz2" title="Link to this heading">¶</a></h1>222<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/bz2.py">Lib/bz2.py</a></p>223<hr class="docutils" />224<p>This module provides a comprehensive interface for compressing and225decompressing data using the bzip2 compression algorithm.</p>226<p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">bz2</span></code> module contains:</p>227<ul class="simple">228<li><p>The <a class="reference internal" href="#bz2.open" title="bz2.open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a> function and <a class="reference internal" href="#bz2.BZ2File" title="bz2.BZ2File"><code class="xref py py-class docutils literal notranslate"><span class="pre">BZ2File</span></code></a> class for reading and229writing compressed files.</p></li>230<li><p>The <a class="reference internal" href="#bz2.BZ2Compressor" title="bz2.BZ2Compressor"><code class="xref py py-class docutils literal notranslate"><span class="pre">BZ2Compressor</span></code></a> and <a class="reference internal" href="#bz2.BZ2Decompressor" title="bz2.BZ2Decompressor"><code class="xref py py-class docutils literal notranslate"><span class="pre">BZ2Decompressor</span></code></a> classes for231incremental (de)compression.</p></li>232<li><p>The <a class="reference internal" href="#bz2.compress" title="bz2.compress"><code class="xref py py-func docutils literal notranslate"><span class="pre">compress()</span></code></a> and <a class="reference internal" href="#bz2.decompress" title="bz2.decompress"><code class="xref py py-func docutils literal notranslate"><span class="pre">decompress()</span></code></a> functions for one-shot233(de)compression.</p></li>234</ul>235<p>This is an <a class="reference internal" href="../glossary.html#term-optional-module"><span class="xref std std-term">optional module</span></a>.236If it is missing from your copy of CPython,237look for documentation from your distributor (that is,238whoever provided Python to you).239If you are the distributor, see <a class="reference internal" href="../using/configure.html#optional-module-requirements"><span class="std std-ref">Requirements for optional modules</span></a>.</p>240<section id="de-compression-of-files">241<h2>(De)compression of files<a class="headerlink" href="#de-compression-of-files" title="Link to this heading">¶</a></h2>242<dl class="py function">243<dt class="sig sig-object py" id="bz2.open">244<span class="sig-prename descclassname"><span class="pre">bz2.</span></span><span class="sig-name descname"><span class="pre">open</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">filename</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mode</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'rb'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">compresslevel</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">9</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">encoding</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">newline</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#bz2.open" title="Link to this definition">¶</a></dt>245<dd><p>Open a bzip2-compressed file in binary or text mode, returning a <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file246object</span></a>.</p>247<p>As with the constructor for <a class="reference internal" href="#bz2.BZ2File" title="bz2.BZ2File"><code class="xref py py-class docutils literal notranslate"><span class="pre">BZ2File</span></code></a>, the <em>filename</em> argument can be248an actual filename (a <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> or <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> object), or an existing249file object to read from or write to.</p>250<p>The <em>mode</em> argument can be any of <code class="docutils literal notranslate"><span class="pre">'r'</span></code>, <code class="docutils literal notranslate"><span class="pre">'rb'</span></code>, <code class="docutils literal notranslate"><span class="pre">'w'</span></code>, <code class="docutils literal notranslate"><span class="pre">'wb'</span></code>,251<code class="docutils literal notranslate"><span class="pre">'x'</span></code>, <code class="docutils literal notranslate"><span class="pre">'xb'</span></code>, <code class="docutils literal notranslate"><span class="pre">'a'</span></code> or <code class="docutils literal notranslate"><span class="pre">'ab'</span></code> for binary mode, or <code class="docutils literal notranslate"><span class="pre">'rt'</span></code>,252<code class="docutils literal notranslate"><span class="pre">'wt'</span></code>, <code class="docutils literal notranslate"><span class="pre">'xt'</span></code>, or <code class="docutils literal notranslate"><span class="pre">'at'</span></code> for text mode. The default is <code class="docutils literal notranslate"><span class="pre">'rb'</span></code>.</p>253<p>The <em>compresslevel</em> argument is an integer from 1 to 9, as for the254<a class="reference internal" href="#bz2.BZ2File" title="bz2.BZ2File"><code class="xref py py-class docutils literal notranslate"><span class="pre">BZ2File</span></code></a> constructor.</p>255<p>For binary mode, this function is equivalent to the <a class="reference internal" href="#bz2.BZ2File" title="bz2.BZ2File"><code class="xref py py-class docutils literal notranslate"><span class="pre">BZ2File</span></code></a>256constructor: <code class="docutils literal notranslate"><span class="pre">BZ2File(filename,</span> <span class="pre">mode,</span> <span class="pre">compresslevel=compresslevel)</span></code>. In257this case, the <em>encoding</em>, <em>errors</em> and <em>newline</em> arguments must not be258provided.</p>259<p>For text mode, a <a class="reference internal" href="#bz2.BZ2File" title="bz2.BZ2File"><code class="xref py py-class docutils literal notranslate"><span class="pre">BZ2File</span></code></a> object is created, and wrapped in an260<a class="reference internal" href="io.html#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.TextIOWrapper</span></code></a> instance with the specified encoding, error261handling behavior, and line ending(s).</p>262<div class="versionadded">263<p><span class="versionmodified added">Added in version 3.3.</span></p>264</div>265<div class="versionchanged">266<p><span class="versionmodified changed">Changed in version 3.4: </span>The <code class="docutils literal notranslate"><span class="pre">'x'</span></code> (exclusive creation) mode was added.</p>267</div>268<div class="versionchanged">269<p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p>270</div>271</dd></dl>272 273<dl class="py class">274<dt class="sig sig-object py" id="bz2.BZ2File">275<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">bz2.</span></span><span class="sig-name descname"><span class="pre">BZ2File</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">filename</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mode</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'r'</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">compresslevel</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">9</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#bz2.BZ2File" title="Link to this definition">¶</a></dt>276<dd><p>Open a bzip2-compressed file in binary mode.</p>277<p>If <em>filename</em> is a <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> or <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> object, open the named file278directly. Otherwise, <em>filename</em> should be a <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a>, which will279be used to read or write the compressed data.</p>280<p>The <em>mode</em> argument can be either <code class="docutils literal notranslate"><span class="pre">'r'</span></code> for reading (default), <code class="docutils literal notranslate"><span class="pre">'w'</span></code> for281overwriting, <code class="docutils literal notranslate"><span class="pre">'x'</span></code> for exclusive creation, or <code class="docutils literal notranslate"><span class="pre">'a'</span></code> for appending. These282can equivalently be given as <code class="docutils literal notranslate"><span class="pre">'rb'</span></code>, <code class="docutils literal notranslate"><span class="pre">'wb'</span></code>, <code class="docutils literal notranslate"><span class="pre">'xb'</span></code> and <code class="docutils literal notranslate"><span class="pre">'ab'</span></code>283respectively.</p>284<p>If <em>filename</em> is a file object (rather than an actual file name), a mode of285<code class="docutils literal notranslate"><span class="pre">'w'</span></code> does not truncate the file, and is instead equivalent to <code class="docutils literal notranslate"><span class="pre">'a'</span></code>.</p>286<p>If <em>mode</em> is <code class="docutils literal notranslate"><span class="pre">'w'</span></code> or <code class="docutils literal notranslate"><span class="pre">'a'</span></code>, <em>compresslevel</em> can be an integer between287<code class="docutils literal notranslate"><span class="pre">1</span></code> and <code class="docutils literal notranslate"><span class="pre">9</span></code> specifying the level of compression: <code class="docutils literal notranslate"><span class="pre">1</span></code> produces the288least compression, and <code class="docutils literal notranslate"><span class="pre">9</span></code> (default) produces the most compression.</p>289<p>If <em>mode</em> is <code class="docutils literal notranslate"><span class="pre">'r'</span></code>, the input file may be the concatenation of multiple290compressed streams.</p>291<p><code class="xref py py-class docutils literal notranslate"><span class="pre">BZ2File</span></code> provides all of the members specified by the292<a class="reference internal" href="io.html#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.BufferedIOBase</span></code></a>, except for <a class="reference internal" href="io.html#io.BufferedIOBase.detach" title="io.BufferedIOBase.detach"><code class="xref py py-meth docutils literal notranslate"><span class="pre">detach()</span></code></a>293and <a class="reference internal" href="io.html#io.IOBase.truncate" title="io.IOBase.truncate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">truncate()</span></code></a>.294Iteration and the <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement are supported.</p>295<p><code class="xref py py-class docutils literal notranslate"><span class="pre">BZ2File</span></code> also provides the following methods and attributes:</p>296<dl class="py method">297<dt class="sig sig-object py" id="bz2.BZ2File.peek">298<span class="sig-name descname"><span class="pre">peek</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#bz2.BZ2File.peek" title="Link to this definition">¶</a></dt>299<dd><p>Return buffered data without advancing the file position. At least one300byte of data will be returned (unless at EOF). The exact number of bytes301returned is unspecified.</p>302<div class="admonition note">303<p class="admonition-title">Note</p>304<p>While calling <code class="xref py py-meth docutils literal notranslate"><span class="pre">peek()</span></code> does not change the file position of305the <code class="xref py py-class docutils literal notranslate"><span class="pre">BZ2File</span></code>, it may change the position of the underlying file306object (e.g. if the <code class="xref py py-class docutils literal notranslate"><span class="pre">BZ2File</span></code> was constructed by passing a file307object for <em>filename</em>).</p>308</div>309<div class="versionadded">310<p><span class="versionmodified added">Added in version 3.3.</span></p>311</div>312</dd></dl>313 314<dl class="py method">315<dt class="sig sig-object py" id="bz2.BZ2File.fileno">316<span class="sig-name descname"><span class="pre">fileno</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#bz2.BZ2File.fileno" title="Link to this definition">¶</a></dt>317<dd><p>Return the file descriptor for the underlying file.</p>318<div class="versionadded">319<p><span class="versionmodified added">Added in version 3.3.</span></p>320</div>321</dd></dl>322 323<dl class="py method">324<dt class="sig sig-object py" id="bz2.BZ2File.readable">325<span class="sig-name descname"><span class="pre">readable</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#bz2.BZ2File.readable" title="Link to this definition">¶</a></dt>326<dd><p>Return whether the file was opened for reading.</p>327<div class="versionadded">328<p><span class="versionmodified added">Added in version 3.3.</span></p>329</div>330</dd></dl>331 332<dl class="py method">333<dt class="sig sig-object py" id="bz2.BZ2File.seekable">334<span class="sig-name descname"><span class="pre">seekable</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#bz2.BZ2File.seekable" title="Link to this definition">¶</a></dt>335<dd><p>Return whether the file supports seeking.</p>336<div class="versionadded">337<p><span class="versionmodified added">Added in version 3.3.</span></p>338</div>339</dd></dl>340 341<dl class="py method">342<dt class="sig sig-object py" id="bz2.BZ2File.writable">343<span class="sig-name descname"><span class="pre">writable</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#bz2.BZ2File.writable" title="Link to this definition">¶</a></dt>344<dd><p>Return whether the file was opened for writing.</p>345<div class="versionadded">346<p><span class="versionmodified added">Added in version 3.3.</span></p>347</div>348</dd></dl>349 350<dl class="py method">351<dt class="sig sig-object py" id="bz2.BZ2File.read1">352<span class="sig-name descname"><span class="pre">read1</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">size</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#bz2.BZ2File.read1" title="Link to this definition">¶</a></dt>353<dd><p>Read up to <em>size</em> uncompressed bytes, while trying to avoid354making multiple reads from the underlying stream. Reads up to a355buffer’s worth of data if size is negative.</p>356<p>Returns <code class="docutils literal notranslate"><span class="pre">b''</span></code> if the file is at EOF.</p>357<div class="versionadded">358<p><span class="versionmodified added">Added in version 3.3.</span></p>359</div>360</dd></dl>361 362<dl class="py method">363<dt class="sig sig-object py" id="bz2.BZ2File.readinto">364<span class="sig-name descname"><span class="pre">readinto</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">b</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#bz2.BZ2File.readinto" title="Link to this definition">¶</a></dt>365<dd><p>Read bytes into <em>b</em>.</p>366<p>Returns the number of bytes read (0 for EOF).</p>367<div class="versionadded">368<p><span class="versionmodified added">Added in version 3.3.</span></p>369</div>370</dd></dl>371 372<dl class="py attribute">373<dt class="sig sig-object py" id="bz2.BZ2File.mode">374<span class="sig-name descname"><span class="pre">mode</span></span><a class="headerlink" href="#bz2.BZ2File.mode" title="Link to this definition">¶</a></dt>375<dd><p><code class="docutils literal notranslate"><span class="pre">'rb'</span></code> for reading and <code class="docutils literal notranslate"><span class="pre">'wb'</span></code> for writing.</p>376<div class="versionadded">377<p><span class="versionmodified added">Added in version 3.13.</span></p>378</div>379</dd></dl>380 381<dl class="py attribute">382<dt class="sig sig-object py" id="bz2.BZ2File.name">383<span class="sig-name descname"><span class="pre">name</span></span><a class="headerlink" href="#bz2.BZ2File.name" title="Link to this definition">¶</a></dt>384<dd><p>The bzip2 file name. Equivalent to the <a class="reference internal" href="io.html#io.FileIO.name" title="io.FileIO.name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code></a>385attribute of the underlying <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a>.</p>386<div class="versionadded">387<p><span class="versionmodified added">Added in version 3.13.</span></p>388</div>389</dd></dl>390 391<div class="versionchanged">392<p><span class="versionmodified changed">Changed in version 3.1: </span>Support for the <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement was added.</p>393</div>394<div class="versionchanged">395<p><span class="versionmodified changed">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"><span class="xref std std-term">file object</span></a> instead of an396actual filename.</p>397<p>The <code class="docutils literal notranslate"><span class="pre">'a'</span></code> (append) mode was added, along with support for reading398multi-stream files.</p>399</div>400<div class="versionchanged">401<p><span class="versionmodified changed">Changed in version 3.4: </span>The <code class="docutils literal notranslate"><span class="pre">'x'</span></code> (exclusive creation) mode was added.</p>402</div>403<div class="versionchanged">404<p><span class="versionmodified changed">Changed in version 3.5: </span>The <a class="reference internal" href="io.html#io.BufferedIOBase.read" title="io.BufferedIOBase.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a> method now accepts an argument of405<code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>406</div>407<div class="versionchanged">408<p><span class="versionmodified changed">Changed in version 3.6: </span>Accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p>409</div>410<div class="versionchanged">411<p><span class="versionmodified changed">Changed in version 3.9: </span>The <em>buffering</em> parameter has been removed. It was ignored and deprecated412since Python 3.0. Pass an open file object to control how the file is413opened.</p>414<p>The <em>compresslevel</em> parameter became keyword-only.</p>415</div>416<div class="versionchanged">417<p><span class="versionmodified changed">Changed in version 3.10: </span>This class is thread unsafe in the face of multiple simultaneous418readers or writers, just like its equivalent classes in <a class="reference internal" href="gzip.html#module-gzip" title="gzip: Interfaces for gzip compression and decompression using file objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gzip</span></code></a> and419<a class="reference internal" href="lzma.html#module-lzma" title="lzma: A Python wrapper for the liblzma compression library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">lzma</span></code></a> have always been.</p>420</div>421</dd></dl>422 423</section>424<section id="incremental-de-compression">425<h2>Incremental (de)compression<a class="headerlink" href="#incremental-de-compression" title="Link to this heading">¶</a></h2>426<dl class="py class">427<dt class="sig sig-object py" id="bz2.BZ2Compressor">428<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">bz2.</span></span><span class="sig-name descname"><span class="pre">BZ2Compressor</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">compresslevel</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">9</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#bz2.BZ2Compressor" title="Link to this definition">¶</a></dt>429<dd><p>Create a new compressor object. This object may be used to compress data430incrementally. For one-shot compression, use the <a class="reference internal" href="#bz2.compress" title="bz2.compress"><code class="xref py py-func docutils literal notranslate"><span class="pre">compress()</span></code></a> function431instead.</p>432<p><em>compresslevel</em>, if given, must be an integer between <code class="docutils literal notranslate"><span class="pre">1</span></code> and <code class="docutils literal notranslate"><span class="pre">9</span></code>. The433default is <code class="docutils literal notranslate"><span class="pre">9</span></code>.</p>434<dl class="py method">435<dt class="sig sig-object py" id="bz2.BZ2Compressor.compress">436<span class="sig-name descname"><span class="pre">compress</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#bz2.BZ2Compressor.compress" title="Link to this definition">¶</a></dt>437<dd><p>Provide data to the compressor object. Returns a chunk of compressed data438if possible, or an empty byte string otherwise.</p>439<p>When you have finished providing data to the compressor, call the440<a class="reference internal" href="#bz2.BZ2Compressor.flush" title="bz2.BZ2Compressor.flush"><code class="xref py py-meth docutils literal notranslate"><span class="pre">flush()</span></code></a> method to finish the compression process.</p>441</dd></dl>442 443<dl class="py method">444<dt class="sig sig-object py" id="bz2.BZ2Compressor.flush">445<span class="sig-name descname"><span class="pre">flush</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#bz2.BZ2Compressor.flush" title="Link to this definition">¶</a></dt>446<dd><p>Finish the compression process. Returns the compressed data left in447internal buffers.</p>448<p>The compressor object may not be used after this method has been called.</p>449</dd></dl>450 451</dd></dl>452 453<dl class="py class">454<dt class="sig sig-object py" id="bz2.BZ2Decompressor">455<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">bz2.</span></span><span class="sig-name descname"><span class="pre">BZ2Decompressor</span></span><a class="headerlink" href="#bz2.BZ2Decompressor" title="Link to this definition">¶</a></dt>456<dd><p>Create a new decompressor object. This object may be used to decompress data457incrementally. For one-shot compression, use the <a class="reference internal" href="#bz2.decompress" title="bz2.decompress"><code class="xref py py-func docutils literal notranslate"><span class="pre">decompress()</span></code></a> function458instead.</p>459<div class="admonition note">460<p class="admonition-title">Note</p>461<p>This class does not transparently handle inputs containing multiple462compressed streams, unlike <a class="reference internal" href="#bz2.decompress" title="bz2.decompress"><code class="xref py py-func docutils literal notranslate"><span class="pre">decompress()</span></code></a> and <a class="reference internal" href="#bz2.BZ2File" title="bz2.BZ2File"><code class="xref py py-class docutils literal notranslate"><span class="pre">BZ2File</span></code></a>. If463you need to decompress a multi-stream input with <code class="xref py py-class docutils literal notranslate"><span class="pre">BZ2Decompressor</span></code>,464you must use a new decompressor for each stream.</p>465</div>466<dl class="py method">467<dt class="sig sig-object py" id="bz2.BZ2Decompressor.decompress">468<span class="sig-name descname"><span class="pre">decompress</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">max_length</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#bz2.BZ2Decompressor.decompress" title="Link to this definition">¶</a></dt>469<dd><p>Decompress <em>data</em> (a <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a>), returning470uncompressed data as bytes. Some of <em>data</em> may be buffered471internally, for use in later calls to <code class="xref py py-meth docutils literal notranslate"><span class="pre">decompress()</span></code>. The472returned data should be concatenated with the output of any473previous calls to <code class="xref py py-meth docutils literal notranslate"><span class="pre">decompress()</span></code>.</p>474<p>If <em>max_length</em> is nonnegative, returns at most <em>max_length</em>475bytes of decompressed data. If this limit is reached and further476output can be produced, the <a class="reference internal" href="#bz2.BZ2Decompressor.needs_input" title="bz2.BZ2Decompressor.needs_input"><code class="xref py py-attr docutils literal notranslate"><span class="pre">needs_input</span></code></a> attribute will477be set to <code class="docutils literal notranslate"><span class="pre">False</span></code>. In this case, the next call to478<code class="xref py py-meth docutils literal notranslate"><span class="pre">decompress()</span></code> may provide <em>data</em> as <code class="docutils literal notranslate"><span class="pre">b''</span></code> to obtain479more of the output.</p>480<p>If all of the input data was decompressed and returned (either481because this was less than <em>max_length</em> bytes, or because482<em>max_length</em> was negative), the <a class="reference internal" href="#bz2.BZ2Decompressor.needs_input" title="bz2.BZ2Decompressor.needs_input"><code class="xref py py-attr docutils literal notranslate"><span class="pre">needs_input</span></code></a> attribute483will be set to <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>484<p>Attempting to decompress data after the end of stream is reached485raises an <a class="reference internal" href="exceptions.html#EOFError" title="EOFError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">EOFError</span></code></a>. Any data found after the end of the486stream is ignored and saved in the <a class="reference internal" href="#bz2.BZ2Decompressor.unused_data" title="bz2.BZ2Decompressor.unused_data"><code class="xref py py-attr docutils literal notranslate"><span class="pre">unused_data</span></code></a> attribute.</p>487<div class="versionchanged">488<p><span class="versionmodified changed">Changed in version 3.5: </span>Added the <em>max_length</em> parameter.</p>489</div>490</dd></dl>491 492<dl class="py attribute">493<dt class="sig sig-object py" id="bz2.BZ2Decompressor.eof">494<span class="sig-name descname"><span class="pre">eof</span></span><a class="headerlink" href="#bz2.BZ2Decompressor.eof" title="Link to this definition">¶</a></dt>495<dd><p><code class="docutils literal notranslate"><span class="pre">True</span></code> if the end-of-stream marker has been reached.</p>496<div class="versionadded">497<p><span class="versionmodified added">Added in version 3.3.</span></p>498</div>499</dd></dl>500 501<dl class="py attribute">502<dt class="sig sig-object py" id="bz2.BZ2Decompressor.unused_data">503<span class="sig-name descname"><span class="pre">unused_data</span></span><a class="headerlink" href="#bz2.BZ2Decompressor.unused_data" title="Link to this definition">¶</a></dt>504<dd><p>Data found after the end of the compressed stream.</p>505<p>If this attribute is accessed before the end of the stream has been506reached, its value will be <code class="docutils literal notranslate"><span class="pre">b''</span></code>.</p>507</dd></dl>508 509<dl class="py attribute">510<dt class="sig sig-object py" id="bz2.BZ2Decompressor.needs_input">511<span class="sig-name descname"><span class="pre">needs_input</span></span><a class="headerlink" href="#bz2.BZ2Decompressor.needs_input" title="Link to this definition">¶</a></dt>512<dd><p><code class="docutils literal notranslate"><span class="pre">False</span></code> if the <a class="reference internal" href="#bz2.BZ2Decompressor.decompress" title="bz2.BZ2Decompressor.decompress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">decompress()</span></code></a> method can provide more513decompressed data before requiring new uncompressed input.</p>514<div class="versionadded">515<p><span class="versionmodified added">Added in version 3.5.</span></p>516</div>517</dd></dl>518 519</dd></dl>520 521</section>522<section id="one-shot-de-compression">523<h2>One-shot (de)compression<a class="headerlink" href="#one-shot-de-compression" title="Link to this heading">¶</a></h2>524<dl class="py function">525<dt class="sig sig-object py" id="bz2.compress">526<span class="sig-prename descclassname"><span class="pre">bz2.</span></span><span class="sig-name descname"><span class="pre">compress</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">compresslevel</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">9</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#bz2.compress" title="Link to this definition">¶</a></dt>527<dd><p>Compress <em>data</em>, a <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a>.</p>528<p><em>compresslevel</em>, if given, must be an integer between <code class="docutils literal notranslate"><span class="pre">1</span></code> and <code class="docutils literal notranslate"><span class="pre">9</span></code>. The529default is <code class="docutils literal notranslate"><span class="pre">9</span></code>.</p>530<p>For incremental compression, use a <a class="reference internal" href="#bz2.BZ2Compressor" title="bz2.BZ2Compressor"><code class="xref py py-class docutils literal notranslate"><span class="pre">BZ2Compressor</span></code></a> instead.</p>531</dd></dl>532 533<dl class="py function">534<dt class="sig sig-object py" id="bz2.decompress">535<span class="sig-prename descclassname"><span class="pre">bz2.</span></span><span class="sig-name descname"><span class="pre">decompress</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#bz2.decompress" title="Link to this definition">¶</a></dt>536<dd><p>Decompress <em>data</em>, a <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a>.</p>537<p>If <em>data</em> is the concatenation of multiple compressed streams, decompress538all of the streams.</p>539<p>For incremental decompression, use a <a class="reference internal" href="#bz2.BZ2Decompressor" title="bz2.BZ2Decompressor"><code class="xref py py-class docutils literal notranslate"><span class="pre">BZ2Decompressor</span></code></a> instead.</p>540<div class="versionchanged">541<p><span class="versionmodified changed">Changed in version 3.3: </span>Support for multi-stream inputs was added.</p>542</div>543</dd></dl>544 545</section>546<section id="examples-of-usage">547<span id="bz2-usage-examples"></span><h2>Examples of usage<a class="headerlink" href="#examples-of-usage" title="Link to this heading">¶</a></h2>548<p>Below are some examples of typical usage of the <code class="xref py py-mod docutils literal notranslate"><span class="pre">bz2</span></code> module.</p>549<p>Using <a class="reference internal" href="#bz2.compress" title="bz2.compress"><code class="xref py py-func docutils literal notranslate"><span class="pre">compress()</span></code></a> and <a class="reference internal" href="#bz2.decompress" title="bz2.decompress"><code class="xref py py-func docutils literal notranslate"><span class="pre">decompress()</span></code></a> to demonstrate round-trip compression:</p>550<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">bz2</span>551<span class="gp">>>> </span><span class="n">data</span> <span class="o">=</span> <span class="sa">b</span><span class="s2">"""</span><span class="se">\</span>552<span class="gp">... </span><span class="s2">Donec rhoncus quis sapien sit amet molestie. Fusce scelerisque vel augue</span>553<span class="gp">... </span><span class="s2">nec ullamcorper. Nam rutrum pretium placerat. Aliquam vel tristique lorem,</span>554<span class="gp">... </span><span class="s2">sit amet cursus ante. In interdum laoreet mi, sit amet ultrices purus</span>555<span class="gp">... </span><span class="s2">pulvinar a. Nam gravida euismod magna, non varius justo tincidunt feugiat.</span>556<span class="gp">... </span><span class="s2">Aliquam pharetra lacus non risus vehicula rutrum. Maecenas aliquam leo</span>557<span class="gp">... </span><span class="s2">felis. Pellentesque semper nunc sit amet nibh ullamcorper, ac elementum</span>558<span class="gp">... </span><span class="s2">dolor luctus. Curabitur lacinia mi ornare consectetur vestibulum."""</span>559<span class="gp">>>> </span><span class="n">c</span> <span class="o">=</span> <span class="n">bz2</span><span class="o">.</span><span class="n">compress</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>560<span class="gp">>>> </span><span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="n">c</span><span class="p">)</span> <span class="c1"># Data compression ratio</span>561<span class="go">1.513595166163142</span>562<span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">bz2</span><span class="o">.</span><span class="n">decompress</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>563<span class="gp">>>> </span><span class="n">data</span> <span class="o">==</span> <span class="n">d</span> <span class="c1"># Check equality to original object after round-trip</span>564<span class="go">True</span>565</pre></div>566</div>567<p>Using <a class="reference internal" href="#bz2.BZ2Compressor" title="bz2.BZ2Compressor"><code class="xref py py-class docutils literal notranslate"><span class="pre">BZ2Compressor</span></code></a> for incremental compression:</p>568<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">bz2</span>569<span class="gp">>>> </span><span class="k">def</span><span class="w"> </span><span class="nf">gen_data</span><span class="p">(</span><span class="n">chunks</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">chunksize</span><span class="o">=</span><span class="mi">1000</span><span class="p">):</span>570<span class="gp">... </span><span class="w"> </span><span class="sd">"""Yield incremental blocks of chunksize bytes."""</span>571<span class="gp">... </span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">chunks</span><span class="p">):</span>572<span class="gp">... </span> <span class="k">yield</span> <span class="sa">b</span><span class="s2">"z"</span> <span class="o">*</span> <span class="n">chunksize</span>573<span class="gp">...</span>574<span class="gp">>>> </span><span class="n">comp</span> <span class="o">=</span> <span class="n">bz2</span><span class="o">.</span><span class="n">BZ2Compressor</span><span class="p">()</span>575<span class="gp">>>> </span><span class="n">out</span> <span class="o">=</span> <span class="sa">b</span><span class="s2">""</span>576<span class="gp">>>> </span><span class="k">for</span> <span class="n">chunk</span> <span class="ow">in</span> <span class="n">gen_data</span><span class="p">():</span>577<span class="gp">... </span> <span class="c1"># Provide data to the compressor object</span>578<span class="gp">... </span> <span class="n">out</span> <span class="o">=</span> <span class="n">out</span> <span class="o">+</span> <span class="n">comp</span><span class="o">.</span><span class="n">compress</span><span class="p">(</span><span class="n">chunk</span><span class="p">)</span>579<span class="gp">...</span>580<span class="gp">>>> </span><span class="c1"># Finish the compression process. Call this once you have</span>581<span class="gp">>>> </span><span class="c1"># finished providing data to the compressor.</span>582<span class="gp">>>> </span><span class="n">out</span> <span class="o">=</span> <span class="n">out</span> <span class="o">+</span> <span class="n">comp</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>583</pre></div>584</div>585<p>The example above uses a very “nonrandom” stream of data586(a stream of <code class="docutils literal notranslate"><span class="pre">b"z"</span></code> chunks). Random data tends to compress poorly,587while ordered, repetitive data usually yields a high compression ratio.</p>588<p>Writing and reading a bzip2-compressed file in binary mode:</p>589<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">bz2</span>590<span class="gp">>>> </span><span class="n">data</span> <span class="o">=</span> <span class="sa">b</span><span class="s2">"""</span><span class="se">\</span>591<span class="gp">... </span><span class="s2">Donec rhoncus quis sapien sit amet molestie. Fusce scelerisque vel augue</span>592<span class="gp">... </span><span class="s2">nec ullamcorper. Nam rutrum pretium placerat. Aliquam vel tristique lorem,</span>593<span class="gp">... </span><span class="s2">sit amet cursus ante. In interdum laoreet mi, sit amet ultrices purus</span>594<span class="gp">... </span><span class="s2">pulvinar a. Nam gravida euismod magna, non varius justo tincidunt feugiat.</span>595<span class="gp">... </span><span class="s2">Aliquam pharetra lacus non risus vehicula rutrum. Maecenas aliquam leo</span>596<span class="gp">... </span><span class="s2">felis. Pellentesque semper nunc sit amet nibh ullamcorper, ac elementum</span>597<span class="gp">... </span><span class="s2">dolor luctus. Curabitur lacinia mi ornare consectetur vestibulum."""</span>598<span class="gp">>>> </span><span class="k">with</span> <span class="n">bz2</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">"myfile.bz2"</span><span class="p">,</span> <span class="s2">"wb"</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>599<span class="gp">... </span> <span class="c1"># Write compressed data to file</span>600<span class="gp">... </span> <span class="n">unused</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>601<span class="gp">...</span>602<span class="gp">>>> </span><span class="k">with</span> <span class="n">bz2</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">"myfile.bz2"</span><span class="p">,</span> <span class="s2">"rb"</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>603<span class="gp">... </span> <span class="c1"># Decompress data from file</span>604<span class="gp">... </span> <span class="n">content</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>605<span class="gp">...</span>606<span class="gp">>>> </span><span class="n">content</span> <span class="o">==</span> <span class="n">data</span> <span class="c1"># Check equality to original object after round-trip</span>607<span class="go">True</span>608</pre></div>609</div>610</section>611</section>612 613 614 <div class="clearer"></div>615 </div>616 </div>617 </div>618 <div class="sphinxsidebar" role="navigation" aria-label="Main">619 <div class="sphinxsidebarwrapper">620 <div>621 <h3><a href="../contents.html">Table of Contents</a></h3>622 <ul>623<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">bz2</span></code> — Support for <strong class="program">bzip2</strong> compression</a><ul>624<li><a class="reference internal" href="#de-compression-of-files">(De)compression of files</a></li>625<li><a class="reference internal" href="#incremental-de-compression">Incremental (de)compression</a></li>626<li><a class="reference internal" href="#one-shot-de-compression">One-shot (de)compression</a></li>627<li><a class="reference internal" href="#examples-of-usage">Examples of usage</a></li>628</ul>629</li>630</ul>631 632 </div>633 <div>634 <h4>Previous topic</h4>635 <p class="topless"><a href="gzip.html"636 title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gzip</span></code> — Support for <strong class="program">gzip</strong> files</a></p>637 </div>638 <div>639 <h4>Next topic</h4>640 <p class="topless"><a href="lzma.html"641 title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">lzma</span></code> — Compression using the LZMA algorithm</a></p>642 </div>643 <script>644 document.addEventListener('DOMContentLoaded', () => {645 const title = document.querySelector('meta[property="og:title"]').content;646 const elements = document.querySelectorAll('.improvepage');647 const pageurl = window.location.href.split('?')[0];648 elements.forEach(element => {649 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));650 url.searchParams.set('pagetitle', title);651 url.searchParams.set('pageurl', pageurl);652 url.searchParams.set('pagesource', "library/bz2.rst");653 element.href = url.toString();654 });655 });656 </script>657 <div role="note" aria-label="source link">658 <h3>This page</h3>659 <ul class="this-page-menu">660 <li><a href="../bugs.html">Report a bug</a></li>661 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>662 <li>663 <a href="https://github.com/python/cpython/blob/main/Doc/library/bz2.rst?plain=1"664 rel="nofollow">Show source665 </a>666 </li>667 668 </ul>669 </div>670 </div>671<div id="sidebarbutton" title="Collapse sidebar">672<span>«</span>673</div>674 675 </div>676 <div class="clearer"></div>677 </div> 678 <div class="related" role="navigation" aria-label="Related">679 <h3>Navigation</h3>680 <ul>681 <li class="right" style="margin-right: 10px">682 <a href="../genindex.html" title="General Index"683 >index</a></li>684 <li class="right" >685 <a href="../py-modindex.html" title="Python Module Index"686 >modules</a> |</li>687 <li class="right" >688 <a href="lzma.html" title="lzma — Compression using the LZMA algorithm"689 >next</a> |</li>690 <li class="right" >691 <a href="gzip.html" title="gzip — Support for gzip files"692 >previous</a> |</li>693 694 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>695 <li><a href="https://www.python.org/">Python</a> »</li>696 <li class="switchers">697 <div class="language_switcher_placeholder"></div>698 <div class="version_switcher_placeholder"></div>699 </li>700 <li>701 702 </li>703 <li id="cpython-language-and-version">704 <a href="../index.html">3.15.0a6 Documentation</a> »705 </li>706 707 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>708 <li class="nav-item nav-item-2"><a href="archiving.html" >Data Compression and Archiving</a> »</li>709 <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">bz2</span></code> — Support for <strong class="program">bzip2</strong> compression</a></li>710 <li class="right">711 712 713 <div class="inline-search" role="search">714 <form class="inline-search" action="../search.html" method="get">715 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">716 <input type="submit" value="Go">717 </form>718 </div>719 |720 </li>721 <li class="right">722<label class="theme-selector-label">723 Theme724 <select class="theme-selector" oninput="activateTheme(this.value)">725 <option value="auto" selected>Auto</option>726 <option value="light">Light</option>727 <option value="dark">Dark</option>728 </select>729</label> |</li>730 731 </ul>732 </div> 733 <div class="footer">734 © <a href="../copyright.html">Copyright</a> 2001 Python Software Foundation.735 <br>736 This page is licensed under the Python Software Foundation License Version 2.737 <br>738 Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.739 <br>740 741 See <a href="/license.html">History and License</a> for more information.<br>742 743 744 <br>745 746 The Python Software Foundation is a non-profit corporation.747<a href="https://www.python.org/psf/donations/">Please donate.</a>748<br>749 <br>750 Last updated on Mar 10, 2026 (08:58 UTC).751 752 <a href="/bugs.html">Found a bug</a>?753 754 <br>755 756 Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.757 </div>758 759 </body>760</html>