matchy/py/files.py

21 lines
498 B
Python
Raw Permalink Normal View History

2024-08-10 10:58:31 +01:00
"""File operation helpers"""
import json
import shutil
2024-08-10 10:58:31 +01:00
def load(file: str) -> dict:
"""Load a json file directly as a dict"""
with open(file) as f:
return json.load(f)
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
"""
intermediate = file + ".nxt"
with open(intermediate, "w") as f:
2024-08-10 10:58:31 +01:00
json.dump(content, f, indent=4)
shutil.move(intermediate, file)