I have a dictionary which I update every time the code runs with a user-created username and password. Whenever I run the script the updated keys and values successfully appear on a JSON file. The problem is that the new dictionary values and keys replace the contents of the JSON file instead of being added to them.
my_dict = {}
# there's code here that utilizes the update method for dictionaries to populate #my_dict with a key and value. Due to its length, I'm not posting it.
with open('data.json', 'r') as f:
json_string = json.dumps(my_dict)
json_string
with open('data.json', 'r') as f:
json.loads(json_string)
with open('data.json', 'w') as f:
f.write(json_string)
#these successfully write and save the data to the data.json file. When I run #the script again, however, the new data replaces the old data in my_dict. I #want the new my_dict data to be added instead. How do I do this?