Thursday, September 13, 2012

Decompress a file using gzip

#!/usr/bin/env python

import gzip
import os

sourceFile = raw_input("Please enter the input compressed file name:")

if (os.path.exists(sourceFile)):
    pass
else:

    print "Sorry, cant find the file! Please check if the filename and path are correct!\n"
    exit(0)

destFile = raw_input("Please enter the output de-compressed file name:")

zipFile = gzip.open(sourceFile,"rb")

unCompressedFile = open(destFile,"wb")

decoded = zipFile.read()

unCompressedFile.write(decoded)

zipFile.close()

unCompressedFile.close()

1 comment:

  1. IOError: [Errno 21] Is a directory:

    I want to extract into a directory as my gzip contains many files and i'm getting the above error. What could be the soution

    ReplyDelete