xyzio

Posts Tagged ‘bzip

Open and print a .gz.bz2 file with Python

leave a comment »

Open and print a file that is compressed with gzip and then with bzip2, i.e a gz.bz2 file, with Python.

import sys
import bz2
import gzip
from cStringIO import StringIO

# .gz.bz2 File is given as commandline argument
filename = sys.argv[1]

gzbzfilename = filename

#Read in the bz2 data
o = open(gzbzfilename, 'rb')
gzbzdata = o.read()

#Decompress the bz2 data
gzdata = bz2.decompress(gzbzdata)

#Next, gunzip the gzip file and read out the file pointer
f = gzip.GzipFile(fileobj=StringIO(gzdata))
file_content = f.read()
f.close()

#Print out the file contents
print file_content

Written by M Kapoor

December 24, 2019 at 3:04 am

Posted in Programming

Tagged with , , ,