From 9e9bde6c54fec0a3143be2062453611d3ad955e7 Mon Sep 17 00:00:00 2001 From: "Eugeniy E. Mikhailov" Date: Thu, 20 Jun 2024 22:49:36 -0400 Subject: separate infer_compression method --- qolab/file_utils/__init__.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'qolab/file_utils') diff --git a/qolab/file_utils/__init__.py b/qolab/file_utils/__init__.py index e61811e..bcc0824 100644 --- a/qolab/file_utils/__init__.py +++ b/qolab/file_utils/__init__.py @@ -69,6 +69,16 @@ def get_next_data_file(prefix, savepath, run_number=None, datestr=None, date_for fname = os.path.join(savepath, f'{prefix}_{datestr}_{run_number:05d}.{extension}') return(fname) +def infer_compression(fname): + """Infers compression algorithm from filename extension""" + compression = None # usual suspect + b, fext = os.path.splitext(fname) + if fext == '.gz': + compression = 'gzip' + elif ( fext == '.bz') or (fext == '.bz2'): + compression = 'bzip' + return compression + def save_table_with_header(fname, data, header='', comment_symbol='%', skip_headers_if_file_exist=False, item_format='e', item_separator='\t', compressionmethod=None, compresslevel=9): """Saves output to CSV or TSV file with specially formatted header. -- cgit v1.2.3