await context.bot.get_file(update.message.document).download(f'./R{p}.txt') AttributeError: 'coroutine' object has no attribute 'download'

Issue

I want to save a file in txt format in my system, but I get an error

async def Getfile(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
    global p, usres
    await context.bot.get_file(await update.message.document).download(f'./R{p}.txt')
    await update.message.reply_text(f"file saved as R{p}.txt")
    usres[int(update.message.from_user.id)] = p
    with open("data.json", "w") as file:
        json.dump(usres, file, indent=4)
    p += 1
    print("ok")

Error : await
context.bot.get_file(update.message.document).download(f’./R{p}.txt’)
AttributeError: ‘coroutine’ object has no attribute ‘download’

Version used

  1. python == 3.8
  2. python-telegram-bot==20.0a1

Solution

This error means that context.bot.get_file(update.message.document) returns a coroutine that needs to be awaited first, so try:

await (await context.bot.get_file(await update.message.document)).download(f'./R{p}.txt')

Answered By – Yevhen Kuzmovych

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published