How to unzip gz file using Python

PythonPython 2.7Gzip

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

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 - Python

From the documentation:

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

Solution 3 - Python

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()

Solution 4 - Python

Not an exact answer because you're using xml data and there is currently no pd.read_xml() function (as of v0.23.4), but pandas (starting with v0.21.0) can uncompress the file for you! Thanks Wes!

import pandas as pd
import os
fn = '../data/file_to_load.json.gz'
print(os.path.isfile(fn))
df = pd.read_json(fn, lines=True, compression='gzip')
df.tail()

Solution 5 - Python

from sh import gunzip

gunzip('/tmp/file1.gz')

Solution 6 - Python

If you are parsing the file after unzipping it, don't forget to use decode() method, is necessary when you open a file as binary.

import gzip
with gzip.open(file.gz, 'rb') as f:
    for line in f:
        print(line.decode().strip())

Solution 7 - Python

It is very simple.. Here you go !!

import gzip

#path_to_file_to_be_extracted

ip = sample.gzip

#output file to be filled

op = open("output_file","w") 

with gzip.open(ip,"rb") as ip_byte:
    op.write(ip_byte.read().decode("utf-8")
    wf.close()

Solution 8 - Python

You can use gzip.decompress() to do it:

  1. read input file using rb mode;
  2. open output file using w mode and utf8 encoding;
  3. gzip.decompress() input bytes;
  4. decode what you get to str.
  5. write str to output file.
def decompress(infile, tofile):
    with open(infile, 'rb') as inf, open(tofile, 'w', encoding='utf8') as tof:
        decom_str = gzip.decompress(inf.read()).decode('utf-8')
        tof.write(decom_str)

Solution 9 - Python

If you have the gzip (and gunzip) programs installed on your computer a simple way is to call that command from python:

import os
filename = 'file.txt.gz'
os.system('gunzip ' + filename)

optionally, if you want to preserve the original file, use

os.system('gunzip --keep ' + filename)

Solution 10 - Python

if you have a linux environment it is very easy to unzip using the command gunzip. go to the file folder and give as below

gunzip file-name 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDarkdeamonView Question on Stackoverflow
Solution 1 - PythonMattView Answer on Stackoverflow
Solution 2 - PythonheinstView Answer on Stackoverflow
Solution 3 - PythonFeiyang.ChenView Answer on Stackoverflow
Solution 4 - Pythonwhs2kView Answer on Stackoverflow
Solution 5 - Pythonperfecto25View Answer on Stackoverflow
Solution 6 - PythonPedro J. SolaView Answer on Stackoverflow
Solution 7 - PythonSURIYA PRAKASHView Answer on Stackoverflow
Solution 8 - PythonsecsilmView Answer on Stackoverflow
Solution 9 - PythonmgbView Answer on Stackoverflow
Solution 10 - PythonarunchiriView Answer on Stackoverflow