TypeError: expected str, bytes or os.PathLike object, not _io.TextIOWrapper

I am trying to open, read, modify, and close a json file using the example here:

How to add a key-value to JSON data retrieved from a file with Python?

import os
import json

path = '/m/shared/Suyash/testdata/BIDS/sub-165/ses-1a/func'
os.chdir(path)

string_filename = "sub-165_ses-1a_task-cue_run-02_bold.json"

with open ("sub-165_ses-1a_task-cue_run-02_bold.json", "r") as jsonFile:
    json_decoded = json.load(jsonFile)

json_decoded["TaskName"] = "CUEEEE"

with open(jsonFile, 'w') as jsonFIle:
    json.dump(json_decoded,jsonFile) ######## error here that open() won't work with _io.TextIOWrapper

I keep getting an error at the end (with open(jsonFile...) that I can’t use the jsonFile variable with open(). I used the exact format as the example provided in the link above so I’m not sure why it’s not working. This is eventually going in a larger script so I want to stay away from hard coding/ using strings for the json file name.

Leave a Comment