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

时间:2017-07-25 10:17:29

标签: python python-3.x python-3.5 python-3.6

我搜索类似的文件名&想要为用户上传它们。

我的问题是,当我想使用Try / Except看到这些文件存在时,如果文件存在,请尝试&其他块工作正常但如果文件不存在,它不会向用户显示文本......有什么问题?

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()  

0 个答案:

没有答案