The is the JSON file:
"name": {
"money": 0,
"email": "none",
"list": ["yes", "no"],
"backpack": {
"books": 0,
"pens": 21
And this is what code I wrote:
# Directory of file
script_dir = os.path.dirname(__file__)
file_path = os.path.join(script_dir, 'C:/Users/dado/files/example.json')
# Opening JSON file
with open(file_path, 'r') as f:
data = json.load(f)
# Defining what total pens is
total_pens = data["name"]["backpack"]["pens"]
# Reducing 1 from total pens (for some reason does not update the file)
total_pens -= 1
# Dumping data (this is mandatory as I heard)
with open(file_path, "w") as f:
json.dump(data,f,indent=4)
# Rest of command below...
Why does "total_pens" in the JSON file not become 20 after I run the code? It should "-= 1" am I right? Please tell me what I did wrong...
I am stuck and do not know what to do