from os import path import json SAVE_FILE = path.join("data", "save.dat") def readSave(*fields): try: with open(SAVE_FILE, 'r') as f: out = {} obj = json.load(f) for field in fields: try: out[field] = obj[field] except KeyError: out[field] = None return out except FileNotFoundError: with open(SAVE_FILE, 'w') as f: json.dump({'acc_token': 0}, f, indent=4) return readSave(*fields) def save(fieldsDic): try: obj = None with open(SAVE_FILE, 'r') as f: out = {} obj = json.load(f) obj.update(fieldsDic) with open(SAVE_FILE, 'w') as f: json.dump(obj, f, indent=4) return True except FileNotFoundError: with open(SAVE_FILE, 'w') as f: json.dump(fieldsDic, f, indent=4) return True