with open(...) closes the file for you, even if something goes wrong. The json module turns Python data into text and back.
import json settings = {"theme": "dark", "volume": 7} with open("settings.json", "w") as f: json.dump(settings, f, indent=2) with open("settings.json") as f: loaded = json.load(f) print(loaded["theme"])