How to unzip gz file using Python

Solution 1:

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)

Solution 2:

From the documentation:

import gzip
with gzip.open('file.txt.gz', 'rb') as f:
    file_content = f.read()

Solution 3:

Maybe you want pass it to pandas also.

with gzip.open('features_train.csv.gz') as f:

    features_train = pd.read_csv(f)

features_train.head()