async def load_abit_class(message: types.Message, state: FSMContext):
async with state.proxy() as abit_data:
abit_data['class'] = int(message.text)
async with open('''D:/Code/data.json''', 'w') as file:
json.dump(abit_data, file)
await state.finish()
That's my code. I am trying to make a tg bot, and I need to write the abit_data dictionary to data.json. But my bot is going to be used by many people and that's so I have to someway write that data avoiding saving conflicts, which can be caused by two people, using the bot at the same moment. I tried to use async but I got traceback:
Task exception was never retrieved
future: <Task finished name='Task-31' coro=<Dispatcher._process_polling_updates() done, defined at C:\Users\vvvpe\Desktop\Connect\venv_2\lib\site-packages\aiogram\dispatcher\dispatcher.py:407> exception=AttributeError('__aenter__')>
Traceback (most recent call last):
File "C:\Users\vvvpe\Desktop\Connect\venv_2\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 415, in _process_polling_updates
for responses in itertools.chain.from_iterable(await self.process_updates(updates, fast)):
File "C:\Users\vvvpe\Desktop\Connect\venv_2\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 235, in process_updates
return await asyncio.gather(*tasks)
File "C:\Users\vvvpe\Desktop\Connect\venv_2\lib\site-packages\aiogram\dispatcher\handler.py", line 116, in notify
response = await handler_obj.handler(*args, **partial_data)
File "C:\Users\vvvpe\Desktop\Connect\venv_2\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 256, in process_update
return await self.message_handlers.notify(update.message)
File "C:\Users\vvvpe\Desktop\Connect\venv_2\lib\site-packages\aiogram\dispatcher\handler.py", line 116, in notify
response = await handler_obj.handler(*args, **partial_data)
File "C:\Users\vvvpe\Desktop\Connect\handlers\General.py", line 77, in load_abit_class
async with open('''D:/Code/data.json''', 'w') as file:
AttributeError: __aenter__
Help, please!
I have the following code to write data to a Json file:
import aiofiles
async with aiofiles.open('D:/Code/data.json', 'w') as file:
await file.write(json.dumps(abit_data, indent=4, ensure_ascii=False))