Last active
August 29, 2015 14:26
-
-
Save indraniel/5aa4ab93933ac7943ccd to your computer and use it in GitHub Desktop.
how to optionally handle and decompress gzipped input files on the fly with python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from __future__ import print_function | |
import gzip | |
class GZopen(object): | |
def __init__(self, filename): | |
f = open(filename) | |
# the file type can be determined with the first 2 bytes | |
magic_number = f.read(2) | |
# rewind the file reader back to the start | |
f.seek(0) | |
if magic_number == '\x1f\x8b': | |
self.f = gzip.GzipFile(fileobj=f) | |
else: | |
self.f = f | |
# methods to use within 'with' blocks | |
def __enter__(self): | |
return self | |
def __exit__(self, type, value, traceback): | |
try: | |
self.f.fileobj.close() | |
except AttributeError: | |
pass | |
finally: | |
self.f.close() | |
# inteface used by python 'opened' files | |
def __getattr__(self, name): | |
return getattr(self.f, name) | |
def __iter__(self): | |
return iter(self.f) | |
def next(self): | |
return next(self.f) | |
if __name__ == '__main__': | |
possible_gzipped_file = sys.argv[1] | |
with GZopen(possible_gzipped_file) as f: | |
for line in f: | |
print(line.rstrip()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment