xyzio

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: