diff --git a/data_file.py b/data_file.py index e3eb0ff..a8091e5 100644 --- a/data_file.py +++ b/data_file.py @@ -1,5 +1,6 @@ #!/usr/bin/python3 +import os import json @@ -102,15 +103,20 @@ class DataclassJSONFile: # these 2 functions do the exact same thing as in the class above def load_dataclass_json(dataclass, file_path: str): - file = open(file_path, "r") - class_dict = json.load(file) - file.close() + if os.path.exists(file_path): + file = open(file_path, "r") + class_dict = json.load(file) + file.close() + + else: + class_dict = {} return dataclass(**class_dict) def save_dataclass_json(class_instance, file_path: str): class_dict = class_instance.__dict__ + class_dict = dict(filter(lambda pair: not callable(pair[1]), class_dict.items())) file = open(file_path, "w") json.dump(class_dict, file)