Skip to content

Instantly share code, notes, and snippets.

@glenrobertson
Created April 19, 2012 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glenrobertson/2424337 to your computer and use it in GitHub Desktop.
Save glenrobertson/2424337 to your computer and use it in GitHub Desktop.
open file depending on file extension: supports .bz2, .zip, .tar, .gz
def fancy_open(filename, mode='r'):
if args.file.endswith('.bz2'):
from bz2 import BZ2File
input_file = BZ2File(filename, mode)
elif args.file.endswith('.zip'):
from zipfile import ZipFile
input_file = ZipFile(filename, mode)
elif args.file.endswith('.tar'):
from tarfile import TarFile
input_file = TarFile(filename, mode)
elif args.file.endswith('.gz'):
from gzip import GzipFile
input_file = GzipFile(filename, mode)
else:
input_file = open(filename, mode)
return input_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment