Reputation: 1
I want to add a new entry in my dictionary but with my code below it overwrites my existing file with the initial dictionary and adds a second dictionary with my new entry. I want to just have one updated dict. My initial dictionary looks like this: Dictionary
This is my code:
@app.route("/add_movie", methods=["POST"])
def add_movie():
test_title = request.form["title"]
test_year = request.form["year"]
new_entry = {"Title": test_title,"Year": test_year,}
with open("movie_database.json", "r+", encoding="UTF-8") as open_file:
movie_database = json.load(open_file)
movie_database.append(new_entry)
json.dump(movie_database, open_file)
return render_template("search.html")
Does anybody know what is wrong here?
Upvotes: 0
Views: 99
Reputation: 61
I believe it's because you used
with open("movie_database.json", "r+", encoding="UTF-8") as open_file:
instead of
with open("movie_database.json", "a", encoding="UTF-8") as open_file:
opening with "a" means append to file
Upvotes: 1