I have a json file
{
"id_1" : "",
"id_2": ""
}
I'm trying to update the value for each using the following function
async def UpdateID(self):
await self.bot.wait_until_ready()
while not self.bot.is_closed():
id1 = 1
id2 = 2
with open("file.json", "r+") as r:
config_json = json.load(r)
config_json.update({"id_1": "%s" %(id1)})
config_json.update({"id_2": "%s" %(id2)})
json.dump(config_json,r)
await asyncio.sleep(120)
Using mode r+
, it copies the file and adds it to the end, thus duplicate all the data instead of replacing. If I use r
, I can't write.
If I use w
or a
, I get an UnsupportedOperation
, not readable
error on the json.load
step. Using w
also makes the file empty.
a+ and w+,
give a JSONDecodeError
,Expecting value: line 1 column 1 (char 0)
error on the json.load
step.
Am I using the wrong mode, or an improper way of fixing the original problem?