Python - 通过多次调用函数节省价值

Python - saving value over multiple calling of function

我多次调用 Python 脚本,我想保存字典的值,但每次调用时它都会被初始化。我在

中定义了该变量
def__init__(self):
    self.newdictt = dict()

这是我正在使用的代码片段:

        my_dict = {"camel": config_file, service: thisisjson_dict}

        self.newdictt.update(my_dict)

        with open('data.json', 'w') as json_file:
            json.dump(self.newdictt, json_file)

但是self.newdictt每次都会被覆盖

您已经将字典保存到文件中。在创建空字典之前,首先检查文件是否存在并读取它。