Home > Mobile >  Python - saving value over multiple calling of function
Python - saving value over multiple calling of function

Time:02-25

I call a Python script multiple times and I want to save value of a dictionary, but it gets initialized every time when it is called. I've defined that variable in

def__init__(self):
    self.newdictt = dict()

This is the piece of code I'm using:

        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)

But self.newdictt gets overwrited every time

CodePudding user response:

You are already saving the dict to a file. Before you create an empty dict, first check if the file exists and read it.

  • Related