Skip to content

Instantly share code, notes, and snippets.

@bradlucas
Last active May 22, 2018 08:59
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradlucas/e1e49f25d14fe2bc424e7c98761012ac to your computer and use it in GitHub Desktop.
Save bradlucas/e1e49f25d14fe2bc424e7c98761012ac to your computer and use it in GitHub Desktop.
Unzip gz file if necessary
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 = f.read(2)
f.seek(0)
f.close()
if magic_number == '\x1f\x8b':
print "gzip file"
data = ''
# with gzip.GzipFile(fileobj=f) as f:
with gzip.open(filename, 'rb') as f:
data = f.read()
print "Writing to " + filename
with open(filename, "w") as f:
f.write(data)
else:
print "csv file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment