0

My exercise idea is to append new value from the input prompt to the list. Instead of adding new value, my list in JSON file always give the newly inputted value.

    import json

    file = 'database.json'

    namelist = []
    name = input("Enter your name:\n")

    with open(file, 'w') as file_object:
        namelist.append(name)
        json.dump(namelist, file_object)
               
  • [This article](https://stackoverflow.com/questions/16208206/confused-by-python-file-mode-w) may help. – Klim Bim Jun 14 '21 at 06:55

1 Answers1

0
with open('database.json', "r+") as file_object:
    data = json.load(file)
    # data.update(your_dict) or data.append(new_data)
    file.seek(0)
    json.dump(data, file)
Klim Bim
  • 484
  • 2
  • 7
  • 2
    Code-only answers are not particularly helpful. Please add some descriptions of how this code solves the problem. – Sven Eberth Jun 14 '21 at 13:19