gooderp18绿色标准版
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

701 line
56KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="bz2 — Support for bzip2 compression" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/bz2.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <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..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <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..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>bz2 — Support for bzip2 compression &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="lzma — Compression using the LZMA algorithm" href="lzma.html" />
  33. <link rel="prev" title="gzip — Support for gzip files" href="gzip.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/bz2.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <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>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <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>
  86. <li><a class="reference internal" href="#de-compression-of-files">(De)compression of files</a></li>
  87. <li><a class="reference internal" href="#incremental-de-compression">Incremental (de)compression</a></li>
  88. <li><a class="reference internal" href="#one-shot-de-compression">One-shot (de)compression</a></li>
  89. <li><a class="reference internal" href="#examples-of-usage">Examples of usage</a></li>
  90. </ul>
  91. </li>
  92. </ul>
  93. </div>
  94. <div>
  95. <h4>Previous topic</h4>
  96. <p class="topless"><a href="gzip.html"
  97. 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>
  98. </div>
  99. <div>
  100. <h4>Next topic</h4>
  101. <p class="topless"><a href="lzma.html"
  102. 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>
  103. </div>
  104. <div role="note" aria-label="source link">
  105. <h3>This Page</h3>
  106. <ul class="this-page-menu">
  107. <li><a href="../bugs.html">Report a Bug</a></li>
  108. <li>
  109. <a href="https://github.com/python/cpython/blob/main/Doc/library/bz2.rst"
  110. rel="nofollow">Show Source
  111. </a>
  112. </li>
  113. </ul>
  114. </div>
  115. </nav>
  116. </div>
  117. </div>
  118. <div class="related" role="navigation" aria-label="related navigation">
  119. <h3>Navigation</h3>
  120. <ul>
  121. <li class="right" style="margin-right: 10px">
  122. <a href="../genindex.html" title="General Index"
  123. accesskey="I">index</a></li>
  124. <li class="right" >
  125. <a href="../py-modindex.html" title="Python Module Index"
  126. >modules</a> |</li>
  127. <li class="right" >
  128. <a href="lzma.html" title="lzma — Compression using the LZMA algorithm"
  129. accesskey="N">next</a> |</li>
  130. <li class="right" >
  131. <a href="gzip.html" title="gzip — Support for gzip files"
  132. accesskey="P">previous</a> |</li>
  133. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  134. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  135. <li class="switchers">
  136. <div class="language_switcher_placeholder"></div>
  137. <div class="version_switcher_placeholder"></div>
  138. </li>
  139. <li>
  140. </li>
  141. <li id="cpython-language-and-version">
  142. <a href="../index.html">3.12.3 Documentation</a> &#187;
  143. </li>
  144. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  145. <li class="nav-item nav-item-2"><a href="archiving.html" accesskey="U">Data Compression and Archiving</a> &#187;</li>
  146. <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>
  147. <li class="right">
  148. <div class="inline-search" role="search">
  149. <form class="inline-search" action="../search.html" method="get">
  150. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  151. <input type="submit" value="Go" />
  152. </form>
  153. </div>
  154. |
  155. </li>
  156. <li class="right">
  157. <label class="theme-selector-label">
  158. Theme
  159. <select class="theme-selector" oninput="activateTheme(this.value)">
  160. <option value="auto" selected>Auto</option>
  161. <option value="light">Light</option>
  162. <option value="dark">Dark</option>
  163. </select>
  164. </label> |</li>
  165. </ul>
  166. </div>
  167. <div class="document">
  168. <div class="documentwrapper">
  169. <div class="bodywrapper">
  170. <div class="body" role="main">
  171. <section id="module-bz2">
  172. <span id="bz2-support-for-bzip2-compression"></span><h1><a class="reference internal" href="#module-bz2" title="bz2: Interfaces for bzip2 compression and decompression."><code class="xref py py-mod docutils literal notranslate"><span class="pre">bz2</span></code></a> — Support for <strong class="program">bzip2</strong> compression<a class="headerlink" href="#module-bz2" title="Link to this heading">¶</a></h1>
  173. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/bz2.py">Lib/bz2.py</a></p>
  174. <hr class="docutils" />
  175. <p>This module provides a comprehensive interface for compressing and
  176. decompressing data using the bzip2 compression algorithm.</p>
  177. <p>The <a class="reference internal" href="#module-bz2" title="bz2: Interfaces for bzip2 compression and decompression."><code class="xref py py-mod docutils literal notranslate"><span class="pre">bz2</span></code></a> module contains:</p>
  178. <ul class="simple">
  179. <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 and
  180. writing compressed files.</p></li>
  181. <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 for
  182. incremental (de)compression.</p></li>
  183. <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-shot
  184. (de)compression.</p></li>
  185. </ul>
  186. <section id="de-compression-of-files">
  187. <h2>(De)compression of files<a class="headerlink" href="#de-compression-of-files" title="Link to this heading">¶</a></h2>
  188. <dl class="py function">
  189. <dt class="sig sig-object py" id="bz2.open">
  190. <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>
  191. <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">file
  192. object</span></a>.</p>
  193. <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 be
  194. an 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 existing
  195. file object to read from or write to.</p>
  196. <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>,
  197. <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>,
  198. <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>
  199. <p>The <em>compresslevel</em> argument is an integer from 1 to 9, as for the
  200. <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>
  201. <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>
  202. constructor: <code class="docutils literal notranslate"><span class="pre">BZ2File(filename,</span> <span class="pre">mode,</span> <span class="pre">compresslevel=compresslevel)</span></code>. In
  203. this case, the <em>encoding</em>, <em>errors</em> and <em>newline</em> arguments must not be
  204. provided.</p>
  205. <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 an
  206. <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, error
  207. handling behavior, and line ending(s).</p>
  208. <div class="versionadded">
  209. <p><span class="versionmodified added">New in version 3.3.</span></p>
  210. </div>
  211. <div class="versionchanged">
  212. <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>
  213. </div>
  214. <div class="versionchanged">
  215. <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>
  216. </div>
  217. </dd></dl>
  218. <dl class="py class">
  219. <dt class="sig sig-object py" id="bz2.BZ2File">
  220. <em class="property"><span class="pre">class</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="o"><span class="pre">*</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.BZ2File" title="Link to this definition">¶</a></dt>
  221. <dd><p>Open a bzip2-compressed file in binary mode.</p>
  222. <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 file
  223. directly. 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 will
  224. be used to read or write the compressed data.</p>
  225. <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> for
  226. overwriting, <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. These
  227. can 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>
  228. respectively.</p>
  229. <p>If <em>filename</em> is a file object (rather than an actual file name), a mode of
  230. <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>
  231. <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 between
  232. <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 the
  233. least compression, and <code class="docutils literal notranslate"><span class="pre">9</span></code> (default) produces the most compression.</p>
  234. <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 multiple
  235. compressed streams.</p>
  236. <p><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> provides all of the members specified by the
  237. <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>
  238. and <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>.
  239. Iteration 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>
  240. <p><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> also provides the following methods:</p>
  241. <dl class="py method">
  242. <dt class="sig sig-object py" id="bz2.BZ2File.peek">
  243. <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>
  244. <dd><p>Return buffered data without advancing the file position. At least one
  245. byte of data will be returned (unless at EOF). The exact number of bytes
  246. returned is unspecified.</p>
  247. <div class="admonition note">
  248. <p class="admonition-title">Note</p>
  249. <p>While calling <a class="reference internal" href="#bz2.BZ2File.peek" title="bz2.BZ2File.peek"><code class="xref py py-meth docutils literal notranslate"><span class="pre">peek()</span></code></a> does not change the file position of
  250. 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>, it may change the position of the underlying file
  251. object (e.g. if 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> was constructed by passing a file
  252. object for <em>filename</em>).</p>
  253. </div>
  254. <div class="versionadded">
  255. <p><span class="versionmodified added">New in version 3.3.</span></p>
  256. </div>
  257. </dd></dl>
  258. <dl class="py method">
  259. <dt class="sig sig-object py" id="bz2.BZ2File.fileno">
  260. <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>
  261. <dd><p>Return the file descriptor for the underlying file.</p>
  262. <div class="versionadded">
  263. <p><span class="versionmodified added">New in version 3.3.</span></p>
  264. </div>
  265. </dd></dl>
  266. <dl class="py method">
  267. <dt class="sig sig-object py" id="bz2.BZ2File.readable">
  268. <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>
  269. <dd><p>Return whether the file was opened for reading.</p>
  270. <div class="versionadded">
  271. <p><span class="versionmodified added">New in version 3.3.</span></p>
  272. </div>
  273. </dd></dl>
  274. <dl class="py method">
  275. <dt class="sig sig-object py" id="bz2.BZ2File.seekable">
  276. <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>
  277. <dd><p>Return whether the file supports seeking.</p>
  278. <div class="versionadded">
  279. <p><span class="versionmodified added">New in version 3.3.</span></p>
  280. </div>
  281. </dd></dl>
  282. <dl class="py method">
  283. <dt class="sig sig-object py" id="bz2.BZ2File.writable">
  284. <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>
  285. <dd><p>Return whether the file was opened for writing.</p>
  286. <div class="versionadded">
  287. <p><span class="versionmodified added">New in version 3.3.</span></p>
  288. </div>
  289. </dd></dl>
  290. <dl class="py method">
  291. <dt class="sig sig-object py" id="bz2.BZ2File.read1">
  292. <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>
  293. <dd><p>Read up to <em>size</em> uncompressed bytes, while trying to avoid
  294. making multiple reads from the underlying stream. Reads up to a
  295. buffer’s worth of data if size is negative.</p>
  296. <p>Returns <code class="docutils literal notranslate"><span class="pre">b''</span></code> if the file is at EOF.</p>
  297. <div class="versionadded">
  298. <p><span class="versionmodified added">New in version 3.3.</span></p>
  299. </div>
  300. </dd></dl>
  301. <dl class="py method">
  302. <dt class="sig sig-object py" id="bz2.BZ2File.readinto">
  303. <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>
  304. <dd><p>Read bytes into <em>b</em>.</p>
  305. <p>Returns the number of bytes read (0 for EOF).</p>
  306. <div class="versionadded">
  307. <p><span class="versionmodified added">New in version 3.3.</span></p>
  308. </div>
  309. </dd></dl>
  310. <div class="versionchanged">
  311. <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>
  312. </div>
  313. <div class="versionchanged">
  314. <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 an
  315. actual filename.</p>
  316. <p>The <code class="docutils literal notranslate"><span class="pre">'a'</span></code> (append) mode was added, along with support for reading
  317. multi-stream files.</p>
  318. </div>
  319. <div class="versionchanged">
  320. <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>
  321. </div>
  322. <div class="versionchanged">
  323. <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 of
  324. <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  325. </div>
  326. <div class="versionchanged">
  327. <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>
  328. </div>
  329. <div class="versionchanged">
  330. <p><span class="versionmodified changed">Changed in version 3.9: </span>The <em>buffering</em> parameter has been removed. It was ignored and deprecated
  331. since Python 3.0. Pass an open file object to control how the file is
  332. opened.</p>
  333. <p>The <em>compresslevel</em> parameter became keyword-only.</p>
  334. </div>
  335. <div class="versionchanged">
  336. <p><span class="versionmodified changed">Changed in version 3.10: </span>This class is thread unsafe in the face of multiple simultaneous
  337. readers 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> and
  338. <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>
  339. </div>
  340. </dd></dl>
  341. </section>
  342. <section id="incremental-de-compression">
  343. <h2>Incremental (de)compression<a class="headerlink" href="#incremental-de-compression" title="Link to this heading">¶</a></h2>
  344. <dl class="py class">
  345. <dt class="sig sig-object py" id="bz2.BZ2Compressor">
  346. <em class="property"><span class="pre">class</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>
  347. <dd><p>Create a new compressor object. This object may be used to compress data
  348. incrementally. 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> function
  349. instead.</p>
  350. <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>. The
  351. default is <code class="docutils literal notranslate"><span class="pre">9</span></code>.</p>
  352. <dl class="py method">
  353. <dt class="sig sig-object py" id="bz2.BZ2Compressor.compress">
  354. <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>
  355. <dd><p>Provide data to the compressor object. Returns a chunk of compressed data
  356. if possible, or an empty byte string otherwise.</p>
  357. <p>When you have finished providing data to the compressor, call the
  358. <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>
  359. </dd></dl>
  360. <dl class="py method">
  361. <dt class="sig sig-object py" id="bz2.BZ2Compressor.flush">
  362. <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>
  363. <dd><p>Finish the compression process. Returns the compressed data left in
  364. internal buffers.</p>
  365. <p>The compressor object may not be used after this method has been called.</p>
  366. </dd></dl>
  367. </dd></dl>
  368. <dl class="py class">
  369. <dt class="sig sig-object py" id="bz2.BZ2Decompressor">
  370. <em class="property"><span class="pre">class</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>
  371. <dd><p>Create a new decompressor object. This object may be used to decompress data
  372. incrementally. 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> function
  373. instead.</p>
  374. <div class="admonition note">
  375. <p class="admonition-title">Note</p>
  376. <p>This class does not transparently handle inputs containing multiple
  377. compressed 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>. If
  378. you need to decompress a multi-stream input with <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>,
  379. you must use a new decompressor for each stream.</p>
  380. </div>
  381. <dl class="py method">
  382. <dt class="sig sig-object py" id="bz2.BZ2Decompressor.decompress">
  383. <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>
  384. <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>), returning
  385. uncompressed data as bytes. Some of <em>data</em> may be buffered
  386. internally, for use in later calls to <a class="reference internal" href="#bz2.decompress" title="bz2.decompress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">decompress()</span></code></a>. The
  387. returned data should be concatenated with the output of any
  388. previous calls to <a class="reference internal" href="#bz2.decompress" title="bz2.decompress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">decompress()</span></code></a>.</p>
  389. <p>If <em>max_length</em> is nonnegative, returns at most <em>max_length</em>
  390. bytes of decompressed data. If this limit is reached and further
  391. output 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 will
  392. be set to <code class="docutils literal notranslate"><span class="pre">False</span></code>. In this case, the next call to
  393. <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> may provide <em>data</em> as <code class="docutils literal notranslate"><span class="pre">b''</span></code> to obtain
  394. more of the output.</p>
  395. <p>If all of the input data was decompressed and returned (either
  396. because this was less than <em>max_length</em> bytes, or because
  397. <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> attribute
  398. will be set to <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
  399. <p>Attempting to decompress data after the end of stream is reached
  400. raises 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 the
  401. stream 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>
  402. <div class="versionchanged">
  403. <p><span class="versionmodified changed">Changed in version 3.5: </span>Added the <em>max_length</em> parameter.</p>
  404. </div>
  405. </dd></dl>
  406. <dl class="py attribute">
  407. <dt class="sig sig-object py" id="bz2.BZ2Decompressor.eof">
  408. <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>
  409. <dd><p><code class="docutils literal notranslate"><span class="pre">True</span></code> if the end-of-stream marker has been reached.</p>
  410. <div class="versionadded">
  411. <p><span class="versionmodified added">New in version 3.3.</span></p>
  412. </div>
  413. </dd></dl>
  414. <dl class="py attribute">
  415. <dt class="sig sig-object py" id="bz2.BZ2Decompressor.unused_data">
  416. <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>
  417. <dd><p>Data found after the end of the compressed stream.</p>
  418. <p>If this attribute is accessed before the end of the stream has been
  419. reached, its value will be <code class="docutils literal notranslate"><span class="pre">b''</span></code>.</p>
  420. </dd></dl>
  421. <dl class="py attribute">
  422. <dt class="sig sig-object py" id="bz2.BZ2Decompressor.needs_input">
  423. <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>
  424. <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 more
  425. decompressed data before requiring new uncompressed input.</p>
  426. <div class="versionadded">
  427. <p><span class="versionmodified added">New in version 3.5.</span></p>
  428. </div>
  429. </dd></dl>
  430. </dd></dl>
  431. </section>
  432. <section id="one-shot-de-compression">
  433. <h2>One-shot (de)compression<a class="headerlink" href="#one-shot-de-compression" title="Link to this heading">¶</a></h2>
  434. <dl class="py function">
  435. <dt class="sig sig-object py" id="bz2.compress">
  436. <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>
  437. <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>
  438. <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>. The
  439. default is <code class="docutils literal notranslate"><span class="pre">9</span></code>.</p>
  440. <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>
  441. </dd></dl>
  442. <dl class="py function">
  443. <dt class="sig sig-object py" id="bz2.decompress">
  444. <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>
  445. <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>
  446. <p>If <em>data</em> is the concatenation of multiple compressed streams, decompress
  447. all of the streams.</p>
  448. <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>
  449. <div class="versionchanged">
  450. <p><span class="versionmodified changed">Changed in version 3.3: </span>Support for multi-stream inputs was added.</p>
  451. </div>
  452. </dd></dl>
  453. </section>
  454. <section id="examples-of-usage">
  455. <span id="bz2-usage-examples"></span><h2>Examples of usage<a class="headerlink" href="#examples-of-usage" title="Link to this heading">¶</a></h2>
  456. <p>Below are some examples of typical usage of the <a class="reference internal" href="#module-bz2" title="bz2: Interfaces for bzip2 compression and decompression."><code class="xref py py-mod docutils literal notranslate"><span class="pre">bz2</span></code></a> module.</p>
  457. <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>
  458. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">bz2</span>
  459. <span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="sa">b</span><span class="s2">&quot;&quot;&quot;</span><span class="se">\</span>
  460. <span class="gp">... </span><span class="s2">Donec rhoncus quis sapien sit amet molestie. Fusce scelerisque vel augue</span>
  461. <span class="gp">... </span><span class="s2">nec ullamcorper. Nam rutrum pretium placerat. Aliquam vel tristique lorem,</span>
  462. <span class="gp">... </span><span class="s2">sit amet cursus ante. In interdum laoreet mi, sit amet ultrices purus</span>
  463. <span class="gp">... </span><span class="s2">pulvinar a. Nam gravida euismod magna, non varius justo tincidunt feugiat.</span>
  464. <span class="gp">... </span><span class="s2">Aliquam pharetra lacus non risus vehicula rutrum. Maecenas aliquam leo</span>
  465. <span class="gp">... </span><span class="s2">felis. Pellentesque semper nunc sit amet nibh ullamcorper, ac elementum</span>
  466. <span class="gp">... </span><span class="s2">dolor luctus. Curabitur lacinia mi ornare consectetur vestibulum.&quot;&quot;&quot;</span>
  467. <span class="gp">&gt;&gt;&gt; </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>
  468. <span class="gp">&gt;&gt;&gt; </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>
  469. <span class="go">1.513595166163142</span>
  470. <span class="gp">&gt;&gt;&gt; </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>
  471. <span class="gp">&gt;&gt;&gt; </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>
  472. <span class="go">True</span>
  473. </pre></div>
  474. </div>
  475. <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>
  476. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">bz2</span>
  477. <span class="gp">&gt;&gt;&gt; </span><span class="k">def</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>
  478. <span class="gp">... </span><span class="w"> </span><span class="sd">&quot;&quot;&quot;Yield incremental blocks of chunksize bytes.&quot;&quot;&quot;</span>
  479. <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>
  480. <span class="gp">... </span> <span class="k">yield</span> <span class="sa">b</span><span class="s2">&quot;z&quot;</span> <span class="o">*</span> <span class="n">chunksize</span>
  481. <span class="gp">...</span>
  482. <span class="gp">&gt;&gt;&gt; </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>
  483. <span class="gp">&gt;&gt;&gt; </span><span class="n">out</span> <span class="o">=</span> <span class="sa">b</span><span class="s2">&quot;&quot;</span>
  484. <span class="gp">&gt;&gt;&gt; </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>
  485. <span class="gp">... </span> <span class="c1"># Provide data to the compressor object</span>
  486. <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>
  487. <span class="gp">...</span>
  488. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># Finish the compression process. Call this once you have</span>
  489. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># finished providing data to the compressor.</span>
  490. <span class="gp">&gt;&gt;&gt; </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>
  491. </pre></div>
  492. </div>
  493. <p>The example above uses a very “nonrandom” stream of data
  494. (a stream of <code class="docutils literal notranslate"><span class="pre">b&quot;z&quot;</span></code> chunks). Random data tends to compress poorly,
  495. while ordered, repetitive data usually yields a high compression ratio.</p>
  496. <p>Writing and reading a bzip2-compressed file in binary mode:</p>
  497. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">bz2</span>
  498. <span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="sa">b</span><span class="s2">&quot;&quot;&quot;</span><span class="se">\</span>
  499. <span class="gp">... </span><span class="s2">Donec rhoncus quis sapien sit amet molestie. Fusce scelerisque vel augue</span>
  500. <span class="gp">... </span><span class="s2">nec ullamcorper. Nam rutrum pretium placerat. Aliquam vel tristique lorem,</span>
  501. <span class="gp">... </span><span class="s2">sit amet cursus ante. In interdum laoreet mi, sit amet ultrices purus</span>
  502. <span class="gp">... </span><span class="s2">pulvinar a. Nam gravida euismod magna, non varius justo tincidunt feugiat.</span>
  503. <span class="gp">... </span><span class="s2">Aliquam pharetra lacus non risus vehicula rutrum. Maecenas aliquam leo</span>
  504. <span class="gp">... </span><span class="s2">felis. Pellentesque semper nunc sit amet nibh ullamcorper, ac elementum</span>
  505. <span class="gp">... </span><span class="s2">dolor luctus. Curabitur lacinia mi ornare consectetur vestibulum.&quot;&quot;&quot;</span>
  506. <span class="gp">&gt;&gt;&gt; </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">&quot;myfile.bz2&quot;</span><span class="p">,</span> <span class="s2">&quot;wb&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  507. <span class="gp">... </span> <span class="c1"># Write compressed data to file</span>
  508. <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>
  509. <span class="gp">...</span>
  510. <span class="gp">&gt;&gt;&gt; </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">&quot;myfile.bz2&quot;</span><span class="p">,</span> <span class="s2">&quot;rb&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  511. <span class="gp">... </span> <span class="c1"># Decompress data from file</span>
  512. <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>
  513. <span class="gp">...</span>
  514. <span class="gp">&gt;&gt;&gt; </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>
  515. <span class="go">True</span>
  516. </pre></div>
  517. </div>
  518. </section>
  519. </section>
  520. <div class="clearer"></div>
  521. </div>
  522. </div>
  523. </div>
  524. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  525. <div class="sphinxsidebarwrapper">
  526. <div>
  527. <h3><a href="../contents.html">Table of Contents</a></h3>
  528. <ul>
  529. <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>
  530. <li><a class="reference internal" href="#de-compression-of-files">(De)compression of files</a></li>
  531. <li><a class="reference internal" href="#incremental-de-compression">Incremental (de)compression</a></li>
  532. <li><a class="reference internal" href="#one-shot-de-compression">One-shot (de)compression</a></li>
  533. <li><a class="reference internal" href="#examples-of-usage">Examples of usage</a></li>
  534. </ul>
  535. </li>
  536. </ul>
  537. </div>
  538. <div>
  539. <h4>Previous topic</h4>
  540. <p class="topless"><a href="gzip.html"
  541. 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>
  542. </div>
  543. <div>
  544. <h4>Next topic</h4>
  545. <p class="topless"><a href="lzma.html"
  546. 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>
  547. </div>
  548. <div role="note" aria-label="source link">
  549. <h3>This Page</h3>
  550. <ul class="this-page-menu">
  551. <li><a href="../bugs.html">Report a Bug</a></li>
  552. <li>
  553. <a href="https://github.com/python/cpython/blob/main/Doc/library/bz2.rst"
  554. rel="nofollow">Show Source
  555. </a>
  556. </li>
  557. </ul>
  558. </div>
  559. </div>
  560. <div id="sidebarbutton" title="Collapse sidebar">
  561. <span>«</span>
  562. </div>
  563. </div>
  564. <div class="clearer"></div>
  565. </div>
  566. <div class="related" role="navigation" aria-label="related navigation">
  567. <h3>Navigation</h3>
  568. <ul>
  569. <li class="right" style="margin-right: 10px">
  570. <a href="../genindex.html" title="General Index"
  571. >index</a></li>
  572. <li class="right" >
  573. <a href="../py-modindex.html" title="Python Module Index"
  574. >modules</a> |</li>
  575. <li class="right" >
  576. <a href="lzma.html" title="lzma — Compression using the LZMA algorithm"
  577. >next</a> |</li>
  578. <li class="right" >
  579. <a href="gzip.html" title="gzip — Support for gzip files"
  580. >previous</a> |</li>
  581. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  582. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  583. <li class="switchers">
  584. <div class="language_switcher_placeholder"></div>
  585. <div class="version_switcher_placeholder"></div>
  586. </li>
  587. <li>
  588. </li>
  589. <li id="cpython-language-and-version">
  590. <a href="../index.html">3.12.3 Documentation</a> &#187;
  591. </li>
  592. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  593. <li class="nav-item nav-item-2"><a href="archiving.html" >Data Compression and Archiving</a> &#187;</li>
  594. <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>
  595. <li class="right">
  596. <div class="inline-search" role="search">
  597. <form class="inline-search" action="../search.html" method="get">
  598. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  599. <input type="submit" value="Go" />
  600. </form>
  601. </div>
  602. |
  603. </li>
  604. <li class="right">
  605. <label class="theme-selector-label">
  606. Theme
  607. <select class="theme-selector" oninput="activateTheme(this.value)">
  608. <option value="auto" selected>Auto</option>
  609. <option value="light">Light</option>
  610. <option value="dark">Dark</option>
  611. </select>
  612. </label> |</li>
  613. </ul>
  614. </div>
  615. <div class="footer">
  616. &copy;
  617. <a href="../copyright.html">
  618. Copyright
  619. </a>
  620. 2001-2024, Python Software Foundation.
  621. <br />
  622. This page is licensed under the Python Software Foundation License Version 2.
  623. <br />
  624. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  625. <br />
  626. See <a href="/license.html">History and License</a> for more information.<br />
  627. <br />
  628. The Python Software Foundation is a non-profit corporation.
  629. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  630. <br />
  631. <br />
  632. Last updated on Apr 09, 2024 (13:47 UTC).
  633. <a href="/bugs.html">Found a bug</a>?
  634. <br />
  635. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  636. </div>
  637. </body>
  638. </html>
上海开阖软件有限公司 沪ICP备12045867号-1