1

So I am getting this error here:

raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not TextIOWrapper

This error happens when I run this code:

def changeusername():
    print("Change Your Username:")
    username = str(input(">>> "))
    with open('settings/userInfo.json', 'w') as f:
        tempholder = json.loads(f)
        tempholder['Username'] *= username
         f.seek(0)
         f.write(json.dumps(tempholder))
    print("Changed Name.")

Essentially I have this JSON file that has a "username" string element in it. I want to change the string element to my input and then save it for later use. The file location is correct and all the code works without the actual JSON function. I have also imported the JSON module. Anyhow, thanks for your help!

Trey Freeman
  • 29
  • 1
  • 7
  • 6
    It should be `json.load`, not `json.loads`. The `s` stands for `string` -- you use that if you've got the JSON string, not a file. – Barmar May 18 '22 at 18:39
  • 1
    See also: https://stackoverflow.com/questions/69270727/how-to-solve-typeerror-the-json-object-must-be-str-bytes-or-bytearray-not-t – Karl Knechtel May 18 '22 at 18:54
  • 1
    I changed what @Barmar recomended but I now get this error.... return loads(fp.read(), io.UnsupportedOperation: not readable – Trey Freeman May 18 '22 at 18:59
  • 1
    You opened in `w` mode, so you can't read it. Also, `w` mode empties the file, so there's nothing to read. If you want to read and write it, use `r+` mode. Don't forget to truncate it after writing. – Barmar May 18 '22 at 19:02
  • But it's best to do it as in the answer, opening it separately for reading and writing. – Barmar May 18 '22 at 19:04

1 Answers1

1

@Barmar excellent comment about json.loads vs json.load is the key to the error. Below some more tips to improve the code to working condition.

In python, it may be tricky to read a file and write content back to it in the same context manager.

A more robust solution may be to use two separate with blocks.

Take a look at this answer, that seems to provide code for what you want to do, albeit without json. https://stackoverflow.com/a/17141572/15092616

The code below works for me

print("Change Your Username:")
username = str(input(">>> "))

with open('settings/userInfo.json', 'r') as f:
    currentFileData = json.load(f)

currentFileData['Username'] = username

with open('settings/userInfo.json', 'w') as f:
    f.write(json.dumps(currentFileData))

print("Changed Name.")

Edits: Thanks for the heads up on reading/writing in the same with block. It is indeed not impossible.

Also adjusted one typo in example code.

Adjusted file location back to Ops original file path in example code.

  • This is not true. The `with` block has nothing to do with this error, that simply makes sure that `.close()` is called. Whether or not you can read *and* write to a file depends on the mode you open it with – juanpa.arrivillaga May 18 '22 at 18:52
  • BUT in this case, you probably should not use read/write mode, IMO. I didn't downvote, btw, but this answer is not technically correct – juanpa.arrivillaga May 18 '22 at 18:53
  • 2
    That a) isn't true (it's just tricky, since writing to a file is always in "overwrite mode" and not "insert mode") and b) is unrelated to the described error. – Karl Knechtel May 18 '22 at 18:53