Writing to JSON creates a TypeError: dump () takes at least 2 arguments (1 given)

I am trying to download a json file. Update it and write it down. Here is my attempt, but I get an error message:

TypeError: dump () accepts at least 2 arguments (1 set)

with open('employees.json') as data_file:
    employees = json.load(data_file)
    data_file.close

employees['employees'].append({
    "id": "2",
    "name": "Rob Croft",
    "key": "0003837852"})

with open('employees.json', 'w') as data_file:
    json.dump(employees)
    data_file.close
+4
source share
1 answer

You forgot to transfer file to file:

json.dump(employees, data_file)

Since you are using the file object as a context manager using an operator with, you do not need to manually close the file. And using is only data_file.closecompletely redundant, since you don't even call the method file.close().

+10
source

Source: https://habr.com/ru/post/1599869/


All Articles