json.loads()
for file data PY-W0078json.loads(open(file, 'r', encoding='utf-8').read())
can be replaced with json.load(open(file, 'r', encoding='utf-8'))
1763 async def playlist_func(self, ctx, *, args=None):
1764 file = f"./music/playlist_{ctx.author.id}.json"
1765 if os.path.isfile(file):
1766 data = json.loads(open(file, "r", encoding="utf-8").read())1767 else:
1768 data = {}
1769 if args is None:
json.loads(f.read())
can be replaced with json.load(f)
29
30 try:
31 with open('emojis.json') as f:
32 self.emojis = json.loads(f.read()) 33 except:
34 raise Exception(
35 "Could not find emojis.json. Make sure to run scrape_emojis.py")
The json
module provides two ways to read JSON data: a .loads()
method that
accepts a JSON string, and a .load()
method, that works on files directly.
So instead of reading a file manually and passing it to json.loads()
, it is
recommended to use json.load()
directly.
with open('data.json') as file:
data = json.loads(file.read()) # Reading file manually
class Socket:
def read_json(self, data):
json.loads(self.socket.read()) # Reading socket manually
self.socket.close()
with open('data.json') as file:
data = json.load(file) # Directly passing the file object
class Socket:
def read_json(self, data):
json.load(self.socket) # Directly passing the socket object
self.socket.close()