Home>

No such file or directory: 'IMG_20211227_220859.jpg' listdir seems to give the desired list of files, and random.choice chooses one, but I don’t understand why it doesn’t find it, is it looking in the wrong place? It seems that it should indicate the full path in the error, but it shows one file name. Help please, I've been banging my head for the second hour.

if call.data== 'foto':
 img_list= os.listdir (configure.config ['path'])
 img_otvet= random.choice (img_list)
 bot.send_photo (chat_id= call.message.chat.id, photo= open (img_otvet, 'rb'))
  • Answer # 1

    listdir only lists filenames. It is necessary to connect the name with the path to the folder:

    img_otvet= os.path.join (configure.config ['path'], random.choice (img_list))
    
  • Answer # 2

    listdir only lists filenames. It is necessary to connect the name with the path to the folder:

    img_otvet= os.path.join (configure.config ['path'], random.choice (img_list))