I'm having trouble with the following code.
It's supposed to eventually create this "ages.json" file (because initially it doesn't exist in the directory.
Then, every time it runs, it increases the age in the file), but that is not happening.
import simplejson as json
import os
# checks if the file exists and if the file is empty
if os.path.isfile("./ages.json") and os.stat("./ages.json").st_size != 0:
old_file = open("./ages.json", "r+")
# loads the file as python readable
data = json.loads(old_file.read())
print("Current age is", data["age"], "-- adding a year.")
data["age"] = data["age"] + 1
print("New age is", data["age"])
#if the file is empty or doesn't exist
else:
old_file = open("./ages.json", "w+")
data = {"name": "Helio", "age": 88}
print("No file Found, setting default age to", data["age"])
# starts at the beginning of the file
old_file.seek(0)
# "dumps" data into a json file
old_file.write(json.dumps(data))