diff --git a/matchy/files/ops.py b/matchy/files/ops.py index a076286..1c159b8 100644 --- a/matchy/files/ops.py +++ b/matchy/files/ops.py @@ -1,6 +1,8 @@ """File operation helpers""" import json import shutil +import pathlib +import os def load(file: str) -> dict: @@ -12,8 +14,12 @@ def load(file: str) -> dict: def save(file: str, content: dict): """ Save out a content dictionary to a file - Stores it in an intermediary file first incase the dump fails """ + # Ensure the save directory exists first + dir = pathlib.Path(os.path.dirname(file)) + dir.mkdir(parents=True, exist_ok=True) + + # Store in an intermediary directory first intermediate = file + ".nxt" with open(intermediate, "w") as f: json.dump(content, f, indent=4)