blob: fdce1e17cf474d2b765de67a7369582a11e0ed42 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
|
"Common file utilities"
from typing import Union
from pathlib import Path
from io import TextIOWrapper
from zipfile import ZipFile, is_zipfile
def open_file(filepath: Union[str, Path]) -> Union[ZipFile, TextIOWrapper]:
"Transparently open both TSV and ZIP files"
if not is_zipfile(filepath):
return open(filepath, encoding="utf-8")
with ZipFile(filepath, "r") as zfile:
return zfile.open(zfile.infolist()[0], "r")
|