title: Python: How to decompress a GZIP file to an uncompressed file on disk?
body: <p>I want to emulate the behavior of <code>gzip -d &lt;file.gz&gt;</code> within a Python script.</p>
  
  <p>The compressed GZIP file is decompressed and written as a file with the same file name as the original GZIP file without the .gz extension.</p>
  
  <p>file.abc.gz --> file.abc</p>
  
  <p>It's not obvious how to do this using the gzip library, all the examples in the docs are for compressing data arrays, and I've not yet found a good example from research. </p>
  
  <h2>Edit</h2>
  
  <p>I've tried the below using tarfile module, but unfortunately it's not working, I think since the GZIP file wasn't created with tar.</p>
  
  <pre><code># get the zipped file's contents list, extract the file
  with tarfile.TarFile(local_zipped_filename) as tar_file:
  
      # list the contents, make sure we only extract the expected named file
      members = tar_file.getmembers()
      for member in members:
          if member.name == filename_unzipped:
              members_to_extract = [member]
              tar_file.extractall(path=destination_dir, members=members_to_extract)
              break   # we've extracted our file, done
  </code></pre>