I am downloading a file with boto3 from AWS S3, it's a basic JSON file.
{
"Counter": 0,
"NumOfReset": 0,
"Highest": 0
}
I can open the JSON file, but when I go to dump it back to the same file after changing some values, I get IOError: [Errno 9] Bad file descriptor
.
with open("/tmp/data.json", "rw") as fh:
data = json.load(fh)
i = data["Counter"]
i = i + 1
if i >= data["Highest"]:
data["Highest"] = i
json.dump(data, fh)
fh.close()
Am I just using the wrong file mode or am I doing this incorrectly?
Two things. Its r+
not rw
, and if you want to overwrite the previous data, you need to return to the beginning of the file, using fh.seek(0)
. Otherwise, the changed JSON string would be appended.
with open("/tmp/data.json", "r+") as fh:
data = json.load(fh)
i = data["Counter"]
i = i + 1
if i >= data["Highest"]:
data["Highest"] = i
fh.seek(0)
json.dump(data, fh)
fh.close()
But that may overwrite the data only partially. So closing and re-opening the file with w
is probably a better idea.
with open("/tmp/data.json", "r") as fh:
data = json.load(fh)
i = data["Counter"]
i = i + 1
if i >= data["Highest"]:
data["Highest"] = i
with open("/tmp/data.json", "w") as fh:
json.dump(data, fh)
fh.close()
No need to fh.close()
, that's what with .. as
is for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With