Created
April 19, 2012 21:29
-
-
Save glenrobertson/2424337 to your computer and use it in GitHub Desktop.
open file depending on file extension: supports .bz2, .zip, .tar, .gz
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
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