How to unzip gz file using Python

Darkdeamon picture Darkdeamon · Jun 24, 2015 · Viewed 118.8k times · Source

I need to extract a gz file that I have downloaded from an FTP site to a local Windows file server. I have the variables set for the local path of the file, and I know it can be used by GZIP muddle.

How can I do this? The file inside the GZ file is an XML file.

Answer

Matt picture Matt · Jun 23, 2017
import gzip
import shutil
with gzip.open('file.txt.gz', 'rb') as f_in:
    with open('file.txt', 'wb') as f_out:
        shutil.copyfileobj(f_in, f_out)