aboutsummaryrefslogtreecommitdiff
path: root/qolab/file_utils
diff options
context:
space:
mode:
Diffstat (limited to 'qolab/file_utils')
-rw-r--r--qolab/file_utils/__init__.py14
1 files changed, 13 insertions, 1 deletions
diff --git a/qolab/file_utils/__init__.py b/qolab/file_utils/__init__.py
index bcc0824..9b7aeb2 100644
--- a/qolab/file_utils/__init__.py
+++ b/qolab/file_utils/__init__.py
@@ -79,7 +79,7 @@ def infer_compression(fname):
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):
+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, match_filename_to_compression=True):
"""Saves output to CSV or TSV file with specially formatted header.
The file is appended if needed.
@@ -105,8 +105,20 @@ def save_table_with_header(fname, data, header='', comment_symbol='%', skip_head
- gzip : gzip method of compression
- bzip : bzip2 method of compression
compresslevel : 9 (default) the highest compression, 0 no compression at all, as it is defined for gzip in Lib/gzip.py
+ match_filename_to_compression: True (default) or False
+ If True changes the filename suffix in accordance with compression method,
+ e.g. 'data.dat' -> 'data.dat.gz' if compression is set to 'gzip',
+ otherwise assumes that users know what they do.
"""
fname = filename2os_fname(fname)
+ compression_infered = infer_compression(fname)
+ if (compression_infered != compressionmethod) and match_filename_to_compression:
+ if compressionmethod is None:
+ fname += '.dat'
+ if compressionmethod == 'gzip':
+ fname += '.gz'
+ elif compressionmethod == 'bzip':
+ fname += '.bz'
file_exist_flag = os.path.exists(fname)
item_format=str.join('', ['{', f':{item_format}', '}'])
_open = open # standard file handler