Python Telegram Bot开发报错:list.append()不接受关键字参数
问题:Telegram Bot发送随机图片组时触发TypeError报错
我正在开发Telegram Bot,需要实现从本地文件夹随机选取10张图片发送给用户的功能,当前代码如下:
def send_certificates(message): text = 'tex' # 图片文件夹路径 path = 'D://PYTHON//PROGRAMM//TelegramBot//img//certificates' # 获取文件夹中的所有文件列表 file_list = os.listdir(path) # 过滤列表,仅保留.jpg或.png格式的文件(或其他支持格式) photo_list = [file for file in file_list if file.endswith('.jpg') or file.endswith('.png')] # 如果文件夹中图片不足10张,输出提示信息 if len(photo_list) < 10: print("There are not enough photos in the folder!") else: # 从列表中随机选取10张图片 selected_photos = random.sample(photo_list, 10) medias = [] # 打印选中的图片名称 for num, png in enumerate(selected_photos): if num == 0: medias.append(InputMediaPhoto(media=png), caption=text) else: medias.append(InputMediaPhoto(media=png)) bot.send_media_group(chat_id=message.chat.id, media=medias)
运行时出现如下错误:
Traceback (most recent call last): File "D:\PYTHON\PROGRAMM\TelegramBot\main.py", line 305, in <module> bot.polling(none_stop=True) File "D:\PYTHON\PROGRAMM\TelegramBot\venv\lib\site-packages\telebot\__init__.py", line 1043, in polling self.__threaded_polling(non_stop=non_stop, interval=interval, timeout=timeout, long_polling_timeout=long_polling_timeout, File "D:\PYTHON\PROGRAMM\TelegramBot\venv\lib\site-packages\telebot\__init__.py", line 1118, in __threaded_polling raise e File "D:\PYTHON\PROGRAMM\TelegramBot\venv\lib\site-packages\telebot\__init__.py", line 1074, in __threaded_polling self.worker_pool.raise_exceptions() File "D:\PYTHON\PROGRAMM\TelegramBot\venv\lib\site-packages\telebot\util.py", line 147, in raise_exceptions raise self.exception_info File "D:\PYTHON\PROGRAMM\TelegramBot\venv\lib\site-packages\telebot\util.py", line 90, in run task(*args, **kwargs) File "D:\PYTHON\PROGRAMM\TelegramBot\venv\lib\site-packages\telebot\__init__.py", line 6788, in _run_middlewares_and_handler result = handler['function'](message) File "D:\PYTHON\PROGRAMM\TelegramBot\main.py", line 299, in info send_certificates(message) File "D:\PYTHON\PROGRAMM\TelegramBot\main.py", line 205, in send_certificates medias.append(InputMediaPhoto(media=png), caption=text) TypeError: list.append() takes no keyword arguments
解决方法
1. 修复报错的直接原因
报错是因为append方法的参数写法错误:你把caption=text作为list.append()的参数传递,但append不接受关键字参数。正确写法是将caption作为InputMediaPhoto的参数放在括号内部:
错误代码:
medias.append(InputMediaPhoto(media=png), caption=text)
修正后:
medias.append(InputMediaPhoto(media=png, caption=text))
2. 修复图片路径缺失问题
当前代码中selected_photos仅存储文件名,没有完整路径,Telebot无法定位本地文件。需要用os.path.join()拼接文件夹路径与文件名,生成完整文件路径:
selected_photos = [os.path.join(path, file) for file in random.sample(photo_list, 10)]
3. 完善图片数量不足的处理逻辑
原代码中图片数量不足时仅打印提示,后续会因遍历未定义的selected_photos触发新报错。需在这种场景下终止函数,并给用户发送提示消息:
if len(photo_list) < 10: bot.send_message(message.chat.id, "文件夹中的图片数量不足10张!") return
修正后的完整代码
import os import random from telebot.types import InputMediaPhoto def send_certificates(message): text = 'tex' # 图片文件夹路径 path = 'D://PYTHON//PROGRAMM//TelegramBot//img//certificates' # 获取文件夹中的所有文件列表 file_list = os.listdir(path) # 过滤列表,仅保留.jpg或.png格式的文件(或其他支持格式) photo_list = [file for file in file_list if file.endswith(('.jpg', '.png'))] # 如果文件夹中图片不足10张,发送提示给用户并终止函数 if len(photo_list) < 10: bot.send_message(message.chat.id, "文件夹中的图片数量不足10张!") return # 从列表中随机选取10张图片,生成完整路径 selected_photos = [os.path.join(path, file) for file in random.sample(photo_list, 10)] medias = [] # 构建媒体组 for num, photo_path in enumerate(selected_photos): if num == 0: medias.append(InputMediaPhoto(media=open(photo_path, 'rb'), caption=text)) else: medias.append(InputMediaPhoto(media=open(photo_path, 'rb'))) # 发送媒体组 bot.send_media_group(chat_id=message.chat.id, media=medias)
注意:Telebot的
InputMediaPhoto接收本地文件时,需要传入打开的二进制文件对象(open(photo_path, 'rb')),直接传路径可能无法识别,因此代码补充了文件打开逻辑。
内容的提问来源于stack exchange,提问作者Kenig
相关产品推荐
相关产品推荐

