From 5e3daec56c73272ebd0af254837c013200fc9dc0 Mon Sep 17 00:00:00 2001 From: Dan Allford Date: Mon, 13 Nov 2023 09:25:54 +0000 Subject: [PATCH] Use os.makedirs to create parent dirs for solution_fn since this is os independent --- linopy/solvers.py | 5 +++-- test/test_optimization.py | 7 +++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/linopy/solvers.py b/linopy/solvers.py index 405f154b..ad13bb73 100644 --- a/linopy/solvers.py +++ b/linopy/solvers.py @@ -171,8 +171,7 @@ def run_cbc( if basis_fn: command += f"-basisO {basis_fn} " - if not os.path.exists(solution_fn): - os.mknod(solution_fn) + os.makedirs(os.path.dirname(solution_fn), exist_ok=True) command = command.strip() @@ -263,6 +262,8 @@ def run_glpk( problem_fn = model.to_file(problem_fn) suffix = problem_fn.suffix[1:] + os.makedirs(os.path.dirname(solution_fn), exist_ok=True) + # TODO use --nopresol argument for non-optimal solution output command = f"glpsol --{suffix} {problem_fn} --output {solution_fn} " if log_fn is not None: diff --git a/test/test_optimization.py b/test/test_optimization.py index 200b6c00..afc12901 100644 --- a/test/test_optimization.py +++ b/test/test_optimization.py @@ -555,6 +555,13 @@ def test_basis_and_warmstart(tmp_path, model, solver, io_api): model.solve(solver, warmstart_fn=basis_fn) +@pytest.mark.parametrize("solver,io_api", params) +def test_solution_fn_parent_dir_doesnt_exist(model, solver, io_api, tmp_path): + solution_fn = tmp_path / "non_existent_dir" / "non_existent_file" + status, condition = model.solve(solver, io_api=io_api, solution_fn=solution_fn) + assert status == "ok" + + # def init_model_large(): # m = Model() # time = pd.Index(range(10), name="time")