json.dumps()
for file data PY-W0079f.write(json.dumps(file_json))
can be replaced with json.dump(file_json, f)
32 message_id = media.id
33 file_json[name] = message_id
34 with open("file_ids.txt", "w") as f:
35 f.write(json.dumps(file_json))36
37
38# Function to reuse to send animation and remember the file_id
f.write(json.dumps({}))
can be replaced with json.dump({}, f)
9
10def reset_file_ids():
11 with open("file_ids.txt", "w") as f:
12 f.write(json.dumps({}))13
14
15if not os.path.exists("file_ids.txt"):
The json
module provides two ways to write JSON data: a .dumps()
method that
accepts a JSON string, and a .dump()
method, that works with files directly.
So instead of using json.dumps()
and writing to a file manually, it is
recommended to use json.dump()
directly.
with open('data.json', 'w') as file:
file.write(json.dumps(data)) # Writing to file manually
class Socket:
def write_data(self, data):
self.socket.write(json.dumps(data)) # Writing to socket manually
self.socket.close()
with open('data.json', 'w') as file:
json.dump(data, file) # Directly passing the file object
class Socket:
def write_data(self, data):
json.dump(data, self.socket) # Directly passing the socket object
self.socket.close()