I had this error and I used the json.dump the wrong way around:
Correct way:
with open("Jello.json", "w") as json_file:
json.dump(object_to_be_saved, json_file)
Wrong way:
I did it the other way around json.dump(json_file, object_to_be_saved)... Thanks Python.
I had this error and I used the json.dump the wrong way around:
Correct way:
with open("Jello.json", "w") as json_file:
json.dump(object_to_be_saved, json_file)
Wrong way:
I did it the other way around json.dump(json_file, object_to_be_saved)... Thanks Python.
You have your loads and dumps backwards, and you should be using load and dump instead (the s suffix means those functions work on strings.). load from a file, dump to a file
users = {}
@client.event
async def on_message(message):
# No need to load the dictionary, our copy is the most correct
await update_data(users, message.author)
await add_experience(users, message.author, 5)
await level_up(users, message.author, message.channel)
with open('users.json', 'w') as f:
json.dump(users, f)
@client.event
async def on_ready():
print ("Ready when you are xd")
print ("I am running on " + client.user.name)
print ("With the ID: " + client.user.id)
# Load the json just once, when the bot starts
global users
with open('users.json') as f:
try:
users = json.load(f)
except:
users = {}
python - Pycord error "TypeError: Object of type TextIOWrapper is not JSON serializable" - Stack Overflow
can't read json file with python. getting type error: json object is 'TextIOWrapper' - Stack Overflow
Json With Python - Stack Overflow
python - TypeError: expected str, bytes or os.PathLike object, not _io.TextIOWrapper - Stack Overflow
json.load() is for loading a file. json.loads() works with strings.
3 ways to load a json file:
import json
import ast
with open(file_path) as file:
data1 = json.load(file)
data2 = json.loads(file.read())
data3 = ast.literal_eval(file.read())
You should use json.load whenever possible, but sometimes the JSON file is not strictly in the correct format (e.g. single quotes instead of double quotes). A solution is to use ast.literal_eval().
import json
name="test.json"
try:
r=open(name)
except FileNotFoundError:
with open(name,"w") as e:
a=input("Enter Name: ")
json.dump(a,e)
json.dump takes data to be written into a file first and the file second.
So the code should be:
# json.dump(data, file)
json.dump(a,e)
https://docs.python.org/3/library/json.html#json.dump
This Question is a bit old, but for anyone with the same issue:
You're right you can't open the jsonFile variable. Its a pointer to another file connection and open wants a string or something similar. Its worth noting that jsonFile should also be closed once you exit the 'with' block so it should not be referenced outside of that.
To answer the question though:
with open(jsonFile, 'w') as jsonFile:
json.dump(json_decoded,jsonFile)
should be
with open(string_filename, 'w') as jsonFile:
json.dump(json_decoded,jsonFile)
You can see we just need to use the same string to open a new connection and then we can give it the same alias we used to read the file if we want. Personally I prefer in_file and out_file just to be explicit about my intent.
If you come here because you are reading in a file from the command line using click and the file cannot be opened/read by with open(click_file_obj, "r") as f, use the name attribute of the _texIOwrapper object generated by click. That is,
with open(click_file_obj.name, "r") as f:
file_text = f.read()
This worked for me. Source: https://python-forum.io/thread-1277.html