From de859807d433632a3a22a3a3c3c60875d4f77235 Mon Sep 17 00:00:00 2001 From: Acribbs Date: Thu, 9 Jan 2025 09:30:12 +0000 Subject: [PATCH] Added suffic to output_file --- cgatcore/experiment.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/cgatcore/experiment.py b/cgatcore/experiment.py index 48929ea4..c2e33dc6 100644 --- a/cgatcore/experiment.py +++ b/cgatcore/experiment.py @@ -1306,11 +1306,27 @@ def stop(logger=None): outfile.close() -def get_output_file(section): +def get_output_file(section, suffix=None): '''return filename to write to, replacing any ``%s`` with section in the output pattern for files (``--output-filename-pattern``). + + Arguments + --------- + section : string + section will replace any %s in the pattern for output files + suffix : string + optional suffix to append to the filename. If provided, it will be + added before any existing extension, or at the end if no extension exists. ''' - return re.sub("%s", section, get_args().output_filename_pattern) + filename = re.sub("%s", section, get_args().output_filename_pattern) + + if suffix: + # Split filename into base and extension (if any) + root, ext = os.path.splitext(filename) + # Add suffix before extension (or at end if no extension) + filename = root + suffix + ext + + return filename def open_output_file(section, mode="w", encoding="utf-8"): @@ -1326,10 +1342,10 @@ def open_output_file(section, mode="w", encoding="utf-8"): Arguments --------- section : string - section will replace any %s in the pattern for output files. + section will replace any %s in the pattern for output files mode : char - file opening mode + file opening mode Returns -------