diff options
Diffstat (limited to 'quality_control/file_utils.py')
-rw-r--r-- | quality_control/file_utils.py | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/quality_control/file_utils.py b/quality_control/file_utils.py new file mode 100644 index 0000000..fdce1e1 --- /dev/null +++ b/quality_control/file_utils.py @@ -0,0 +1,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") |