Skip to content

Commit ef68a2a

Browse files
authored
open file using context manager to properly close it again (#381)
1 parent 04c9e52 commit ef68a2a

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

linopy/solvers.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,12 @@ def path_to_string(path: Path) -> str:
162162

163163

164164
def read_sense_from_problem_file(problem_fn: Path | str):
165-
f = open(problem_fn).read()
166-
if read_io_api_from_problem_file(problem_fn) == "lp":
165+
with open(problem_fn) as file:
166+
f = file.read()
167+
file_format = read_io_api_from_problem_file(problem_fn)
168+
if file_format == "lp":
167169
return "min" if "min" in f.lower() else "max"
168-
elif read_io_api_from_problem_file(problem_fn) == "mps":
170+
elif file_format == "mps":
169171
return "max" if "OBJSENSE\n MAX\n" in f else "min"
170172
else:
171173
msg = "Unsupported problem file format."

0 commit comments

Comments
 (0)