Sophie

Sophie

distrib > Mageia > 6 > armv5tl > by-pkgid > a600cd26dfe6bfd8c11f12bce5cb0eee > files > 781

python3-docs-3.5.3-1.1.mga6.noarch.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>13.2. gzip — Support for gzip files &mdash; Python 3.5.3 documentation</title>
    
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.5.3',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python 3.5.3 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python 3.5.3 documentation" href="../contents.html" />
    <link rel="up" title="13. Data Compression and Archiving" href="archiving.html" />
    <link rel="next" title="13.3. bz2 — Support for bzip2 compression" href="bz2.html" />
    <link rel="prev" title="13.1. zlib — Compression compatible with gzip" href="zlib.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    <script type="text/javascript" src="../_static/version_switch.js"></script>
    
    
 

  </head>
  <body role="document">  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="bz2.html" title="13.3. bz2 — Support for bzip2 compression"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="zlib.html" title="13.1. zlib — Compression compatible with gzip"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
        <li>
          <span class="version_switcher_placeholder">3.5.3</span>
          <a href="../index.html">Documentation </a> &raquo;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li class="nav-item nav-item-2"><a href="archiving.html" accesskey="U">13. Data Compression and Archiving</a> &raquo;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="Quick search" type="text" name="q" />
          <input type="submit" value="Go" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-gzip">
<span id="gzip-support-for-gzip-files"></span><h1>13.2. <a class="reference internal" href="#module-gzip" title="gzip: Interfaces for gzip compression and decompression using file objects."><code class="xref py py-mod docutils literal"><span class="pre">gzip</span></code></a> &#8212; Support for <strong class="program">gzip</strong> files<a class="headerlink" href="#module-gzip" title="Permalink to this headline">¶</a></h1>
<p><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.5/Lib/gzip.py">Lib/gzip.py</a></p>
<hr class="docutils" />
<p>This module provides a simple interface to compress and decompress files just
like the GNU programs <strong class="program">gzip</strong> and <strong class="program">gunzip</strong> would.</p>
<p>The data compression is provided by the <a class="reference internal" href="zlib.html#module-zlib" title="zlib: Low-level interface to compression and decompression routines compatible with gzip."><code class="xref py py-mod docutils literal"><span class="pre">zlib</span></code></a> module.</p>
<p>The <a class="reference internal" href="#module-gzip" title="gzip: Interfaces for gzip compression and decompression using file objects."><code class="xref py py-mod docutils literal"><span class="pre">gzip</span></code></a> module provides the <a class="reference internal" href="#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal"><span class="pre">GzipFile</span></code></a> class, as well as the
<a class="reference internal" href="#gzip.open" title="gzip.open"><code class="xref py py-func docutils literal"><span class="pre">open()</span></code></a>, <a class="reference internal" href="#gzip.compress" title="gzip.compress"><code class="xref py py-func docutils literal"><span class="pre">compress()</span></code></a> and <a class="reference internal" href="#gzip.decompress" title="gzip.decompress"><code class="xref py py-func docutils literal"><span class="pre">decompress()</span></code></a> convenience functions.
The <a class="reference internal" href="#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal"><span class="pre">GzipFile</span></code></a> class reads and writes <strong class="program">gzip</strong>-format files,
automatically compressing or decompressing the data so that it looks like an
ordinary <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a>.</p>
<p>Note that additional file formats which can be decompressed by the
<strong class="program">gzip</strong> and <strong class="program">gunzip</strong> programs, such  as those produced by
<strong class="program">compress</strong> and <strong class="program">pack</strong>, are not supported by this module.</p>
<p>The module defines the following items:</p>
<dl class="function">
<dt id="gzip.open">
<code class="descclassname">gzip.</code><code class="descname">open</code><span class="sig-paren">(</span><em>filename</em>, <em>mode='rb'</em>, <em>compresslevel=9</em>, <em>encoding=None</em>, <em>errors=None</em>, <em>newline=None</em><span class="sig-paren">)</span><a class="headerlink" href="#gzip.open" title="Permalink to this definition">¶</a></dt>
<dd><p>Open a gzip-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
object</span></a>.</p>
<p>The <em>filename</em> argument can be an actual filename (a <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal"><span class="pre">str</span></code></a> or
<a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a> object), or an existing file object to read from or write to.</p>
<p>The <em>mode</em> argument can be any of <code class="docutils literal"><span class="pre">'r'</span></code>, <code class="docutils literal"><span class="pre">'rb'</span></code>, <code class="docutils literal"><span class="pre">'a'</span></code>, <code class="docutils literal"><span class="pre">'ab'</span></code>,
<code class="docutils literal"><span class="pre">'w'</span></code>, <code class="docutils literal"><span class="pre">'wb'</span></code>, <code class="docutils literal"><span class="pre">'x'</span></code> or <code class="docutils literal"><span class="pre">'xb'</span></code> for binary mode, or <code class="docutils literal"><span class="pre">'rt'</span></code>,
<code class="docutils literal"><span class="pre">'at'</span></code>, <code class="docutils literal"><span class="pre">'wt'</span></code>, or <code class="docutils literal"><span class="pre">'xt'</span></code> for text mode. The default is <code class="docutils literal"><span class="pre">'rb'</span></code>.</p>
<p>The <em>compresslevel</em> argument is an integer from 0 to 9, as for the
<a class="reference internal" href="#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal"><span class="pre">GzipFile</span></code></a> constructor.</p>
<p>For binary mode, this function is equivalent to the <a class="reference internal" href="#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal"><span class="pre">GzipFile</span></code></a>
constructor: <code class="docutils literal"><span class="pre">GzipFile(filename,</span> <span class="pre">mode,</span> <span class="pre">compresslevel)</span></code>. In this case, the
<em>encoding</em>, <em>errors</em> and <em>newline</em> arguments must not be provided.</p>
<p>For text mode, a <a class="reference internal" href="#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal"><span class="pre">GzipFile</span></code></a> object is created, and wrapped in an
<a class="reference internal" href="io.html#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal"><span class="pre">io.TextIOWrapper</span></code></a> instance with the specified encoding, error
handling behavior, and line ending(s).</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.3: </span>Added support for <em>filename</em> being a file object, support for text mode,
and the <em>encoding</em>, <em>errors</em> and <em>newline</em> arguments.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>Added support for the <code class="docutils literal"><span class="pre">'x'</span></code>, <code class="docutils literal"><span class="pre">'xb'</span></code> and <code class="docutils literal"><span class="pre">'xt'</span></code> modes.</p>
</div>
</dd></dl>

<dl class="class">
<dt id="gzip.GzipFile">
<em class="property">class </em><code class="descclassname">gzip.</code><code class="descname">GzipFile</code><span class="sig-paren">(</span><em>filename=None</em>, <em>mode=None</em>, <em>compresslevel=9</em>, <em>fileobj=None</em>, <em>mtime=None</em><span class="sig-paren">)</span><a class="headerlink" href="#gzip.GzipFile" title="Permalink to this definition">¶</a></dt>
<dd><p>Constructor for the <a class="reference internal" href="#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal"><span class="pre">GzipFile</span></code></a> class, which simulates most of the
methods of a <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a>, with the exception of the <code class="xref py py-meth docutils literal"><span class="pre">truncate()</span></code>
method.  At least one of <em>fileobj</em> and <em>filename</em> must be given a non-trivial
value.</p>
<p>The new class instance is based on <em>fileobj</em>, which can be a regular file, an
<a class="reference internal" href="io.html#io.BytesIO" title="io.BytesIO"><code class="xref py py-class docutils literal"><span class="pre">io.BytesIO</span></code></a> object, or any other object which simulates a file.  It
defaults to <code class="docutils literal"><span class="pre">None</span></code>, in which case <em>filename</em> is opened to provide a file
object.</p>
<p>When <em>fileobj</em> is not <code class="docutils literal"><span class="pre">None</span></code>, the <em>filename</em> argument is only used to be
included in the <strong class="program">gzip</strong> file header, which may include the original
filename of the uncompressed file.  It defaults to the filename of <em>fileobj</em>, if
discernible; otherwise, it defaults to the empty string, and in this case the
original filename is not included in the header.</p>
<p>The <em>mode</em> argument can be any of <code class="docutils literal"><span class="pre">'r'</span></code>, <code class="docutils literal"><span class="pre">'rb'</span></code>, <code class="docutils literal"><span class="pre">'a'</span></code>, <code class="docutils literal"><span class="pre">'ab'</span></code>, <code class="docutils literal"><span class="pre">'w'</span></code>,
<code class="docutils literal"><span class="pre">'wb'</span></code>, <code class="docutils literal"><span class="pre">'x'</span></code>, or <code class="docutils literal"><span class="pre">'xb'</span></code>, depending on whether the file will be read or
written.  The default is the mode of <em>fileobj</em> if discernible; otherwise, the
default is <code class="docutils literal"><span class="pre">'rb'</span></code>.</p>
<p>Note that the file is always opened in binary mode. To open a compressed file
in text mode, use <a class="reference internal" href="#gzip.open" title="gzip.open"><code class="xref py py-func docutils literal"><span class="pre">open()</span></code></a> (or wrap your <a class="reference internal" href="#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal"><span class="pre">GzipFile</span></code></a> with an
<a class="reference internal" href="io.html#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal"><span class="pre">io.TextIOWrapper</span></code></a>).</p>
<p>The <em>compresslevel</em> argument is an integer from <code class="docutils literal"><span class="pre">0</span></code> to <code class="docutils literal"><span class="pre">9</span></code> controlling
the level of compression; <code class="docutils literal"><span class="pre">1</span></code> is fastest and produces the least
compression, and <code class="docutils literal"><span class="pre">9</span></code> is slowest and produces the most compression. <code class="docutils literal"><span class="pre">0</span></code>
is no compression. The default is <code class="docutils literal"><span class="pre">9</span></code>.</p>
<p>The <em>mtime</em> argument is an optional numeric timestamp to be written to
the last modification time field in the stream when compressing.  It
should only be provided in compression mode.  If omitted or <code class="docutils literal"><span class="pre">None</span></code>, the
current time is used.  See the <a class="reference internal" href="#gzip.GzipFile.mtime" title="gzip.GzipFile.mtime"><code class="xref py py-attr docutils literal"><span class="pre">mtime</span></code></a> attribute for more details.</p>
<p>Calling a <a class="reference internal" href="#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal"><span class="pre">GzipFile</span></code></a> object&#8217;s <code class="xref py py-meth docutils literal"><span class="pre">close()</span></code> method does not close
<em>fileobj</em>, since you might wish to append more material after the compressed
data.  This also allows you to pass an <a class="reference internal" href="io.html#io.BytesIO" title="io.BytesIO"><code class="xref py py-class docutils literal"><span class="pre">io.BytesIO</span></code></a> object opened for
writing as <em>fileobj</em>, and retrieve the resulting memory buffer using the
<a class="reference internal" href="io.html#io.BytesIO" title="io.BytesIO"><code class="xref py py-class docutils literal"><span class="pre">io.BytesIO</span></code></a> object&#8217;s <a class="reference internal" href="io.html#io.BytesIO.getvalue" title="io.BytesIO.getvalue"><code class="xref py py-meth docutils literal"><span class="pre">getvalue()</span></code></a> method.</p>
<p><a class="reference internal" href="#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal"><span class="pre">GzipFile</span></code></a> supports the <a class="reference internal" href="io.html#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal"><span class="pre">io.BufferedIOBase</span></code></a> interface,
including iteration and the <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal"><span class="pre">with</span></code></a> statement.  Only the
<code class="xref py py-meth docutils literal"><span class="pre">truncate()</span></code> method isn&#8217;t implemented.</p>
<p><a class="reference internal" href="#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal"><span class="pre">GzipFile</span></code></a> also provides the following method and attribute:</p>
<dl class="method">
<dt id="gzip.GzipFile.peek">
<code class="descname">peek</code><span class="sig-paren">(</span><em>n</em><span class="sig-paren">)</span><a class="headerlink" href="#gzip.GzipFile.peek" title="Permalink to this definition">¶</a></dt>
<dd><p>Read <em>n</em> uncompressed bytes without advancing the file position.
At most one single read on the compressed stream is done to satisfy
the call.  The number of bytes returned may be more or less than
requested.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">While calling <a class="reference internal" href="#gzip.GzipFile.peek" title="gzip.GzipFile.peek"><code class="xref py py-meth docutils literal"><span class="pre">peek()</span></code></a> does not change the file position of
the <a class="reference internal" href="#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal"><span class="pre">GzipFile</span></code></a>, it may change the position of the underlying
file object (e.g. if the <a class="reference internal" href="#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal"><span class="pre">GzipFile</span></code></a> was constructed with the
<em>fileobj</em> parameter).</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.2.</span></p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="gzip.GzipFile.mtime">
<code class="descname">mtime</code><a class="headerlink" href="#gzip.GzipFile.mtime" title="Permalink to this definition">¶</a></dt>
<dd><p>When decompressing, the value of the last modification time field in
the most recently read header may be read from this attribute, as an
integer.  The initial value before reading any headers is <code class="docutils literal"><span class="pre">None</span></code>.</p>
<p>All <strong class="program">gzip</strong> compressed streams are required to contain this
timestamp field.  Some programs, such as <strong class="program">gunzip</strong>, make use
of the timestamp.  The format is the same as the return value of
<a class="reference internal" href="time.html#time.time" title="time.time"><code class="xref py py-func docutils literal"><span class="pre">time.time()</span></code></a> and the <a class="reference internal" href="os.html#os.stat_result.st_mtime" title="os.stat_result.st_mtime"><code class="xref py py-attr docutils literal"><span class="pre">st_mtime</span></code></a> attribute of
the object returned by <a class="reference internal" href="os.html#os.stat" title="os.stat"><code class="xref py py-func docutils literal"><span class="pre">os.stat()</span></code></a>.</p>
</dd></dl>

<div class="versionchanged">
<p><span class="versionmodified">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"><span class="pre">with</span></code></a> statement was added, along with the
<em>mtime</em> constructor argument and <a class="reference internal" href="#gzip.GzipFile.mtime" title="gzip.GzipFile.mtime"><code class="xref py py-attr docutils literal"><span class="pre">mtime</span></code></a> attribute.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.2: </span>Support for zero-padded and unseekable files was added.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.3: </span>The <a class="reference internal" href="io.html#io.BufferedIOBase.read1" title="io.BufferedIOBase.read1"><code class="xref py py-meth docutils literal"><span class="pre">io.BufferedIOBase.read1()</span></code></a> method is now implemented.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>Added support for the <code class="docutils literal"><span class="pre">'x'</span></code> and <code class="docutils literal"><span class="pre">'xb'</span></code> modes.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.5: </span>Added support for writing arbitrary
<a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like objects</span></a>.
The <a class="reference internal" href="io.html#io.BufferedIOBase.read" title="io.BufferedIOBase.read"><code class="xref py py-meth docutils literal"><span class="pre">read()</span></code></a> method now accepts an argument of
<code class="docutils literal"><span class="pre">None</span></code>.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="gzip.compress">
<code class="descclassname">gzip.</code><code class="descname">compress</code><span class="sig-paren">(</span><em>data</em>, <em>compresslevel=9</em><span class="sig-paren">)</span><a class="headerlink" href="#gzip.compress" title="Permalink to this definition">¶</a></dt>
<dd><p>Compress the <em>data</em>, returning a <a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a> object containing
the compressed data.  <em>compresslevel</em> has the same meaning as in
the <a class="reference internal" href="#gzip.GzipFile" title="gzip.GzipFile"><code class="xref py py-class docutils literal"><span class="pre">GzipFile</span></code></a> constructor above.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.2.</span></p>
</div>
</dd></dl>

<dl class="function">
<dt id="gzip.decompress">
<code class="descclassname">gzip.</code><code class="descname">decompress</code><span class="sig-paren">(</span><em>data</em><span class="sig-paren">)</span><a class="headerlink" href="#gzip.decompress" title="Permalink to this definition">¶</a></dt>
<dd><p>Decompress the <em>data</em>, returning a <a class="reference internal" href="functions.html#bytes" title="bytes"><code class="xref py py-class docutils literal"><span class="pre">bytes</span></code></a> object containing the
uncompressed data.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.2.</span></p>
</div>
</dd></dl>

<div class="section" id="examples-of-usage">
<span id="gzip-usage-examples"></span><h2>13.2.1. Examples of usage<a class="headerlink" href="#examples-of-usage" title="Permalink to this headline">¶</a></h2>
<p>Example of how to read a compressed file:</p>
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">gzip</span>
<span class="k">with</span> <span class="n">gzip</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">&#39;/home/joe/file.txt.gz&#39;</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
    <span class="n">file_content</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
</pre></div>
</div>
<p>Example of how to create a compressed GZIP file:</p>
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">gzip</span>
<span class="n">content</span> <span class="o">=</span> <span class="n">b</span><span class="s2">&quot;Lots of content here&quot;</span>
<span class="k">with</span> <span class="n">gzip</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">&#39;/home/joe/file.txt.gz&#39;</span><span class="p">,</span> <span class="s1">&#39;wb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
    <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">content</span><span class="p">)</span>
</pre></div>
</div>
<p>Example of how to GZIP compress an existing file:</p>
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">gzip</span>
<span class="kn">import</span> <span class="nn">shutil</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">&#39;/home/joe/file.txt&#39;</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f_in</span><span class="p">:</span>
    <span class="k">with</span> <span class="n">gzip</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">&#39;/home/joe/file.txt.gz&#39;</span><span class="p">,</span> <span class="s1">&#39;wb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f_out</span><span class="p">:</span>
        <span class="n">shutil</span><span class="o">.</span><span class="n">copyfileobj</span><span class="p">(</span><span class="n">f_in</span><span class="p">,</span> <span class="n">f_out</span><span class="p">)</span>
</pre></div>
</div>
<p>Example of how to GZIP compress a binary string:</p>
<div class="highlight-python3"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">gzip</span>
<span class="n">s_in</span> <span class="o">=</span> <span class="n">b</span><span class="s2">&quot;Lots of content here&quot;</span>
<span class="n">s_out</span> <span class="o">=</span> <span class="n">gzip</span><span class="o">.</span><span class="n">compress</span><span class="p">(</span><span class="n">s_in</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt>Module <a class="reference internal" href="zlib.html#module-zlib" title="zlib: Low-level interface to compression and decompression routines compatible with gzip."><code class="xref py py-mod docutils literal"><span class="pre">zlib</span></code></a></dt>
<dd>The basic data compression module needed to support the <strong class="program">gzip</strong> file
format.</dd>
</dl>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">13.2. <code class="docutils literal"><span class="pre">gzip</span></code> &#8212; Support for <strong class="program">gzip</strong> files</a><ul>
<li><a class="reference internal" href="#examples-of-usage">13.2.1. Examples of usage</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="zlib.html"
                        title="previous chapter">13.1. <code class="docutils literal"><span class="pre">zlib</span></code> &#8212; Compression compatible with <strong class="program">gzip</strong></a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="bz2.html"
                        title="next chapter">13.3. <code class="docutils literal"><span class="pre">bz2</span></code> &#8212; Support for <strong class="program">bzip2</strong> compression</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">Report a Bug</a></li>
      <li><a href="../_sources/library/gzip.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="bz2.html" title="13.3. bz2 — Support for bzip2 compression"
             >next</a> |</li>
        <li class="right" >
          <a href="zlib.html" title="13.1. zlib — Compression compatible with gzip"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
        <li>
          <span class="version_switcher_placeholder">3.5.3</span>
          <a href="../index.html">Documentation </a> &raquo;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li class="nav-item nav-item-2"><a href="archiving.html" >13. Data Compression and Archiving</a> &raquo;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="Quick search" type="text" name="q" />
          <input type="submit" value="Go" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 2001-2017, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Jan 20, 2017.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
    </div>

  </body>
</html>