Value not writing to correct key in JSON
h
RogueHalo (402)

Hey There,

So I'm trying to increment a value in my JSON file. The idea is that I can call a function (dayInc() which is in game.py) and it will increment the value of the key called "days" (in stats.json)

However, it just seems to be writing to the end of the JSON file where I'm trying to increment the value of the key "days"

Not sure what is going wrong with it though, any help would be appreciated!

Note: As you can see (from what the program outputs), it increments fine in the python file, just not in the JSON file.

You are viewing a single comment. View All
brandosha (12)

@JacksonCowie Ok. It looks like you open the file, read it - which moves the stream to the end of the file - and then write starting at the end of the file. Use file_object.truncate(0) and file_object.seek(0) before the json.dump To clear the file and then write starting from the beginning of the file.