Oct-30-2021, 12:16 PM
file_data = json.load(file)When you do this
file_data
it's a dictionary.Then on line-11 can not use
.append
as that method is only for list.For dictionary can use
.update
Don't use
try: except
with return nothing,when test out stuff want to see all errors.Quick test working.
import json from pprint import pprint def write_json(new_date, filename='data.json'): with open(filename, 'r+') as file: file_data = json.load(file) #print(file_data) file_data["scripts"].update(new_date) #pprint(file_data) file.seek(0) json.dump(file_data, file, indent=4) if __name__ == '__main__': y = { "emp_name": "Nikhil", "email": "[email protected]", "job_profile": "Full Time", } write_json(y)