Python: How to decompress a GZIP file to an uncompressed file on disk?

import gzip, shutil

with gzip.open('file.abc.gz', 'r') as f_in, open('file.abc', 'wb') as f_out:
  shutil.copyfileobj(f_in, f_out)

The gzip module provides a file-like object with the decompressed content of a gzip file; the shutil module provides a convenient helper for copying content from one file-like object to another.


This is a simple inversion of an example given in the official documentation:

Example of how to GZIP compress an existing file:

import gzip
import shutil
with open('/home/joe/file.txt', 'rb') as f_in:
    with gzip.open('/home/joe/file.txt.gz', 'wb') as f_out:
        shutil.copyfileobj(f_in, f_out)