Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bad file descriptor in Python 2.7

Tags:

python

json

file

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?

like image 704
SiennaD. Avatar asked May 13 '16 15:05

SiennaD.


1 Answers

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.

like image 146
C14L Avatar answered Oct 21 '22 11:10

C14L