Skip to content

Commit 4b05518

Browse files
committed
Add custom IOError msg for file not found in loaddata
1 parent 0f4c540 commit 4b05518

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/diffpy/utils/parsers/loaddata.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#
1414
##############################################################################
1515

16+
import os
17+
1618
import numpy
1719

1820

@@ -99,6 +101,10 @@ def countcolumnsvalues(line):
99101
nc = nv = 0
100102
return nc, nv
101103

104+
# Check if file exists before trying to open
105+
if not os.path.exists(filename):
106+
raise IOError(f"File {filename} cannot be found. Please rerun the program specifying a valid filename.")
107+
102108
# make sure fid gets cleaned up
103109
with open(filename, "rb") as fid:
104110
# search for the start of datablock

tests/diffpy/utils/parsers/test_loaddata.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ def test_loadData_default(datafile):
1515
d2c = np.array([[3, 31], [4, 32], [5, 33]])
1616

1717
with pytest.raises(IOError) as err:
18-
loadData("doesnotexist")
19-
assert "No such file or directory" in str(err.value)
18+
loadData("doesnotexist.txt")
19+
assert (
20+
str(err.value)
21+
== "File doesnotexist.txt cannot be found. Please rerun the program specifying a valid filename."
22+
)
2023

2124
# The default minrows=10 makes it read from the third line
2225
d = loadData(loaddata01)

0 commit comments

Comments
 (0)