Skip to content

Instantly share code, notes, and snippets.

@whosaysni
Created August 5, 2014 04:50
Show Gist options
  • Save whosaysni/2e3398bef09289be438a to your computer and use it in GitHub Desktop.
Save whosaysni/2e3398bef09289be438a to your computer and use it in GitHub Desktop.
Zとgzに対応する: loading both Z/gz compressed file
# 汚くて発狂しそう
def open_zfiles(filename):
open_method = open # builtin open
if filename.endswith('gz'):
open_method = gzip.open # gzip's open
elif filename.endswith('Z'):
from subprocess import Popen, PIPE
open_method = lambda fn: Popen(['zcat', fn], stdout=PIPE).stdout
with open_method(filename, 'rb') as infile:
return infile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment