ошибка UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
выводит ошибку UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte и указывает на строку with open(downloaded_file, "rb") as image2string:
вот код def'а с ошибкой:
def news_photo(message: types.ChatPhoto, name_text: list ):
file_info = bot.get_file(message.photo[0].file_id)
downloaded_file = bot.download_file(file_info.file_path)
with open(downloaded_file, "rb") as image2string:
converted_string = base64.b64encode(image2string.read())
print(converted_string)
весь код команды:
@bot.message_handler(commands=['new_news'])
def new_news(message: types.Message):
bot.send_message(message.from_user.id, '!!! Названия новостей не должны повторяться !!!\n!!! Обязательно проверять на орфографические ошибки !!!')
bot.send_message(message.from_user.id, 'Напишите название новости')
bot.register_next_step_handler(message, news_name)
def news_name(message: types.Message):
bot.send_message(message.from_user.id, 'Напишите текст новости')
bot.register_next_step_handler(message, news_text, message.text)
def news_text(message: types.Message, name: str):
name_text = [name,message.text]
bot.send_message(message.from_user.id, 'Отправьте фото для новости')
bot.register_next_step_handler(message, news_photo, name_text)
def news_photo(message: types.ChatPhoto, name_text: list ):
file_info = bot.get_file(message.photo[0].file_id)
downloaded_file = bot.download_file(file_info.file_path)
with open(downloaded_file, "rb") as image2string:
converted_string = base64.b64encode(image2string.read())
bot.send_message(message.from_user.id, f'название: {name_text[0]}\nтекст: {name_text[1]}\nфото b64: {converted_string}')
полная ошибка:
Traceback (most recent call last):
File "d:\all_scripts\site\bot_news_tg.py", line 88, in <module>
bot.polling(none_stop=True)
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\telebot\__init__.py", line 621, in polling
self.__threaded_polling(non_stop=non_stop, interval=interval, timeout=timeout, long_polling_timeout=long_polling_timeout,
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\telebot\__init__.py", line 695, in __threaded_polling
raise e
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\telebot\__init__.py", line 651, in __threaded_polling
self.worker_pool.raise_exceptions()
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\telebot\util.py", line 147, in raise_exceptions
raise self.exception_info
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\telebot\util.py", line 93, in run
task(*args, **kwargs)
File "d:\all_scripts\site\bot_news_tg.py", line 78, in news_photo
with open(downloaded_file, "rb") as image2string:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte