From 32a3a89e10ac2cbbadc9b20c053950b5f648d7f6 Mon Sep 17 00:00:00 2001 From: Tamalampudi Siva Harsha Vardhan Reddy <2400030361@kluniversity.in> Date: Mon, 24 Nov 2025 11:59:52 +0530 Subject: [PATCH 1/2] Implement open_data_file function with error handlingfeat: Add open_data_file() function to shared library - Issue #251 Added a function to open and read a data file with error handling. --- scripts/shared.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/scripts/shared.py b/scripts/shared.py index 541988fc..1bddf086 100644 --- a/scripts/shared.py +++ b/scripts/shared.py @@ -369,3 +369,47 @@ def update_readme( logger.info( f"Updated README with new image and description for {entry_title}." ) + +def open_data_file(file_path): + """ + Open and read a data file with proper error handling. + + If a process or report script attempts to open a file that does not exist, + an exception is generated and converted into a helpful error message. + + Args: + file_path (str): Path to the data file to open + + Returns: + str: Content of the file + + Raises: + QuantifyingException: If file doesn't exist or can't be opened + """ + try: + if not os.path.exists(file_path): + raise QuantifyingException( + f"Data file not found: {file_path}", + exit_code=1 + ) + + with open(file_path, 'r', encoding='utf-8') as f: + return f.read() + + except QuantifyingException: + raise + except FileNotFoundError as e: + raise QuantifyingException( + f"Data file not found: {file_path}", + exit_code=1 + ) + except PermissionError as e: + raise QuantifyingException( + f"Permission denied when accessing data file: {file_path}", + exit_code=1 + ) + except Exception as e: + raise QuantifyingException( + f"Error opening data file {file_path}: {str(e)}", + exit_code=1 + ) From e5cbcb4647cd42b84b2eb0faf76162a582726758 Mon Sep 17 00:00:00 2001 From: Tamalampudi Siva Harsha Vardhan Reddy <2400030361@kluniversity.in> Date: Mon, 24 Nov 2025 16:05:15 +0530 Subject: [PATCH 2/2] fix: remove duplicate file existence check in open_data_file() Removed redundant os.path.exists() check before file open. The FileNotFoundError exception handler already covers this case, making the explicit check unnecessary. --- scripts/shared.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/scripts/shared.py b/scripts/shared.py index 1bddf086..2c2c8c94 100644 --- a/scripts/shared.py +++ b/scripts/shared.py @@ -387,11 +387,6 @@ def open_data_file(file_path): QuantifyingException: If file doesn't exist or can't be opened """ try: - if not os.path.exists(file_path): - raise QuantifyingException( - f"Data file not found: {file_path}", - exit_code=1 - ) with open(file_path, 'r', encoding='utf-8') as f: return f.read()