Skip to content

Commit 999972b

Browse files
committed
Refactor open_data_file to handle all errors within try/except
1 parent 1a13e79 commit 999972b

1 file changed

Lines changed: 5 additions & 7 deletions

File tree

scripts/shared.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,17 @@ def open_data_file(logger, file_path, usecols=None):
7373
expected errors into QuantifyingException.
7474
This function is shared so all process/report
7575
scripts use the same error behavior.
76-
7776
"""
77+
try:
78+
# Attempt reading the file
79+
return pd.read_csv(file_path, usecols=usecols)
7880

7981
# File does not exist
80-
if not os.path.isfile(file_path):
82+
except FileNotFoundError:
8183
raise QuantifyingException(
8284
message=f"Data file not found: {file_path}", exit_code=1
8385
)
8486

85-
try:
86-
# Reading the file
87-
return pd.read_csv(file_path, usecols=usecols)
88-
8987
# Empty or invalid CSV file
9088
except pd.errors.EmptyDataError:
9189
raise QuantifyingException(
@@ -99,7 +97,7 @@ def open_data_file(logger, file_path, usecols=None):
9997
exit_code=1,
10098
)
10199

102-
# Any other unexpected issue
100+
# Any other unexpected issue
103101
except Exception as e:
104102
raise QuantifyingException(
105103
message=f"Unexpected error opening file '{file_path}': {str(e)}",

0 commit comments

Comments
 (0)