Brad Lucas

A blog mostly about programming
May 17, 2018

Unzip File If Necessary

Ran into a situation where an external vendor started sending csv files gzipped. The trouble was they started zipping only larger files. Since the system that ingested the files would break on a gz file I needed a routine to unzip the gz files and leave the non-zipped files alone.

    def unzip_if_necessary(self, filename):
        print "unzip_if_necessary: " + filename
        f = open(filename)

        # Read magic number (the first 2 bytes) and rewind.                                                                                                                                                                               
        magic_number =

        if magic_number == '\x1f\x8b':
            print "gzip file"
            data = ''
            # with  gzip.GzipFile(fileobj=f) as f:                                                                                                                                                                                        
            with, 'rb') as f:
                data =
            print "Writing to " + filename
            with open(filename, "w") as f:
            print "csv file"


Tags: python