Ensure the path directory is created before saving a json file
This commit is contained in:
parent
04e9e3a5b1
commit
0e81951f75
1 changed files with 7 additions and 1 deletions
|
@ -1,6 +1,8 @@
|
||||||
"""File operation helpers"""
|
"""File operation helpers"""
|
||||||
import json
|
import json
|
||||||
import shutil
|
import shutil
|
||||||
|
import pathlib
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
def load(file: str) -> dict:
|
def load(file: str) -> dict:
|
||||||
|
@ -12,8 +14,12 @@ def load(file: str) -> dict:
|
||||||
def save(file: str, content: dict):
|
def save(file: str, content: dict):
|
||||||
"""
|
"""
|
||||||
Save out a content dictionary to a file
|
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"
|
intermediate = file + ".nxt"
|
||||||
with open(intermediate, "w") as f:
|
with open(intermediate, "w") as f:
|
||||||
json.dump(content, f, indent=4)
|
json.dump(content, f, indent=4)
|
||||||
|
|
Loading…
Add table
Reference in a new issue