Ensure the path directory is created before saving a json file

This commit is contained in:
Marc Di Luzio 2024-08-16 10:08:00 +01:00
parent 04e9e3a5b1
commit 0e81951f75

View file

@ -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)