1

I search for similar file names & want to upload them for user.

My problem is that when I want to use Try/Except to see this files exists, If files exists, Try & Else Blocks works correctly But If file doesn't exists, It doesn't show user the text ... What is the problem ?

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()  
  • 3
    Why wouldn't the file exist? You got the file name from `os.listdir`, so the file must exist. Are you saying someone deleted the file after the call to `listdir` but before the call to `open`? – Aran-Fey Jul 25 '17 at 10:21
  • @Rawing First Part :OK, So How should I Check that if the file is not in os.listdir? Second Part : No, Files Are Consistent ... –  Jul 25 '17 at 10:27
  • 1
    https://stackoverflow.com/questions/15032108/ may help – Glenn Rogers Jul 25 '17 at 10:37
  • @GlennRogers Thanks But It didn't help me ... –  Jul 25 '17 at 10:43

0 Answers0