但在Python中,如果文件不存在则不响应

2024-09-22 14:25:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我搜索相似的文件名并希望为用户上传它们。你知道吗

我的问题是,当我想使用Try/Except查看此文件是否存在时,如果文件存在,Try&Else块会正常工作,但如果文件不存在,它不会向用户显示文本。。。有什么问题?你知道吗

search = str(args[0])
pattern = re.compile('.*%s.*\.pdf' %search, re.I)
for files in filter(pattern.search, os.listdir('.')):

    try:
        requested_file = open(files, 'rb')
    except IOError:
        bot.sendChatAction(chat_id, 'typing')
        bot.sendMessage(chat_id, "This File Doesn't Exists") 
    else:  
        bot.sendChatAction(chat_id, 'typing')
        bot.sendMessage(chat_id, "The File You Needed : " + files)    
        bot.sendChatAction(chat_id, 'upload_document')
        bot.sendDocument(chat_id, requested_file, files)
        requested_file.close()  

Tags: 文件用户reidtypingsearchbotchat