Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

[aiofiles & asyncio dont write logs in file.txt

The question is as to why it does not write certain logs to the file, errors are not shown.
Bot for tg on aiogram.

logs.py:

import aiofiles
import asyncio

async def writelog(user_id: int, log: str):
    return 
    async with aiofiles.open('assets/recently.txt', mode='w') as f:
        await f.write(f'{user_id}:{log}\n')

async def readlogs():
    return ''
    async with aiofiles.open('assets/recently.txt', mode='r') as f:
        text = await f.read()
    return text

The code for writing a log to a file from script:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

await writelog(message.from_user.id, 'turn off notificationsđź””')

help with the code, please🤍

>Solution :

You are returning from the functions, that’s why it is not working.

Change the code to this

import aiofiles
import asyncio

async def writelog(user_id: int, log: str):
    async with aiofiles.open('assets/recently.txt', mode='w') as f:
        await f.write(f'{user_id}:{log}\n')

async def readlogs():
    async with aiofiles.open('assets/recently.txt', mode='r') as f:
        text = await f.read()
    return text

If you don’t have complexity in your code and if you are calling the functions in a right manner, then this must work.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading