S
S
smokedotpy2021-10-28 17:46:40
Python
smokedotpy, 2021-10-28 17:46:40

How to get a number and substitute it in the file path in aiogram?

When entering a number
Error:

FileNotFoundError: [Errno 2] No such file or directory: 'img/butuzov8/[8] Геометрия - Рабочая тетрадь Бутузов, Кадомцев, Прасолов.jpg'

My not working code:
@dp.message_handler(content_types=['text'], text='[8] Геометрия - Рабочая тетрадь Бутузов, Кадомцев, Прасолов')
async def alg8mordnom(message: types.Message):
    await message.answer(text="<b>Введите номер, например: </b>\n<code>27</code>", parse_mode="HTML")
    await bot.send_photo(photo=types.InputFile("img/butuzov8/"+message.text+".jpg"), chat_id=message.chat.id, caption="<b>️ Для выбора учебника, испозьзуйте клавиатуру ниже.</b>", parse_mode="HTML", reply_markup=kb.geometry8book)


How to get the number and substitute it in the file path

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-10-28
@smokedotpy

Did you read the error? It directly says that the specified file (image) was not found.
If you're sure the file exists, read on.
The path you specified is relative , so it is relative to the current working directory (which may or may not match the directory where the script is located, depending on how you run it).
So use pathlib to form the absolute path to the image - i.e. starting at the root of the drive. If the file you are looking for is not far from the bot directory, then you can take advantage of the fact that sys.argv[0] (sys module) contains the path to the bot script.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question