【发布时间】:2021-04-24 08:55:31
【问题描述】:
我正在使用 Telegram 机器人下载文件,我当前的代码如下所示:
def file_handler(Update, context: CallbackContext):
file = context.bot.getFile(Update.message.document.file_id)
print("file_id: " + str(Update.message.document.file_id))
file.download(custom_path='C:/Users/User/Desktop/python/projects/Telegram-Database/files')
def main():
updater = Updater(token, use_context=True)
dispatcher = updater.dispatcher
dispatcher.add_handler(MessageHandler(Filters.document, file_handler))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
当我这样做时:
file.download()
它下载当前目录中的文件,但我想下载文件夹中的所有文件。所以我在这里查找了文档:https://python-telegram-bot.readthedocs.io/en/stable/telegram.file.html,实际上我可以设置自定义路径来下载该文件夹中的文件。但是,当我使用我当前的代码时,我得到一个权限错误:
with open(filename, 'wb') as fobj:
PermissionError: [Errno 13] Permission denied: 'C:/Users/User/Desktop/python/projects/Telegram-Database/files'
我不知道为什么。我尝试重新创建文件夹并重新启动程序,但我总是遇到同样的错误。我在 Telegram 机器人中查找了这个特定的错误,但没有显示任何内容。你能帮忙吗?
【问题讨论】:
-
不能写入文件夹,
C:/Users/User/Desktop/python/projects/Telegram-Database/files是文件夹,不是文件。 -
Ohhhhhhhhhhhhhhh,所以在这种情况下,我只是继续覆盖文件?
-
尝试类似:
file.download(custom_path='C:/Users/User/Desktop/python/projects/Telegram-Database/files/file1.txt') -
你能把它作为答案让我选择吗?