def anyfile(infile, mode='r', encoding="utf8"):
'''
return a file handler with the support for gzip/zip comppressed files
if infile is a two value tuple, then first one is the compressed file;
the second one is the actual filename in the compressed file.
e.g., ('a.zip', 'aa.txt')
'''
if isinstance(infile, tuple):
infile, rawfile = infile[:2]
else:
rawfile = os.path.splitext(infile)[0]
filetype = os.path.splitext(infile)[1].lower()
if filetype == '.gz':
import gzip
in_f = io.TextIOWrapper(gzip.GzipFile(infile, 'r'),encoding=encoding)
elif filetype == '.zip':
import zipfile
in_f = io.TextIOWrapper(zipfile.ZipFile(infile, 'r').open(rawfile, 'r'),encoding=encoding)
else:
in_f = open(infile, mode, encoding=encoding)
return in_f
评论列表
文章目录