Z
Z
zeni1agent2020-02-26 20:34:58
Python
zeni1agent, 2020-02-26 20:34:58

Why does the bot give the error No such file or directory: 'temp/?

I decided to start the bot without any errors and everything starts fine, but as soon as I upload the file, the bot gives the error
line 28 No such file or directory: 'temp/86f7181e-cef6-4809-b61c-e61df0f43fc9.png'.

import time
import telebot
import requests

TOKEN = "1067228659:AAHYjLiwxqsdfdTUQwVbgq6IlqFuYZQ4T_d49M"
KERAS_REST_API_URL = ""

bot = telebot.TeleBot(TOKEN)


@bot.message_handler(commands=['start'])
def send_welcome(message):
    bot.reply_to(message, 'Hey Start')


@bot.message_handler(commands=['info'])
def send_welcome(message):
    info = ('Hey Info')
    bot.reply_to(message, info)


import uuid

@bot.message_handler(content_types=["photo"])
def answer_photo(message):
    photo = bot.get_file(message.photo[-1].file_id)
    # URL direction to image
    photo_url = "https://api.telegram.org/file/bot{0}/{1}".format(
        TOKEN, photo.file_path)
    # Computer Vision parameters
    r = requests.get(photo_url)
    file_name = str(uuid.uuid4()) + '.png'
    if r.status_code == 200:
        with open('temp/' + file_name, 'wb') as f:
            f.write(r.content)
    else:
        bot.reply_to(message, 'something fails...')
        return

    img = open('temp/' + file_name, 'rb')

    #img = open('inpred.png', 'rb')

    payload = {"image":img}

    bot.send_chat_action(message.chat.id, 'typing')
    try:
        r = requests.post(KERAS_REST_API_URL, files=payload).json()
    except:
        bot.reply_to(message, 'something fails....')
    print(r)
    time.sleep(1)

    img_path = None
    try:
        if r['success']:
            img_path = r['result_path']
            img_result = open(img_path, 'rb')
            bot.reply_to(message, photo_url)
            bot.send_photo(message.chat.id, img_result, reply_to_message_id=message.message_id)

            img_path = r['mask_path']
            img_result = open(img_path, 'rb')
            bot.reply_to(message, photo_url)
            bot.send_photo(message.chat.id, img_result, reply_to_message_id=message.message_id)
      
            img_path = r['cg_path']
            img_result = open(img_path, 'rb')
            bot.reply_to(message, photo_url)
            bot.send_photo(message.chat.id, img_result, reply_to_message_id=message.message_id)
        else:
            bot.reply_to(message, 'something fails...')
    except:
        bot.reply_to(message, 'something fails...')

@bot.message_handler(func=lambda m: True)
def reply_all(message):
    if message.chat.type == "private":
        bot.reply_to(message, 'Please send me an image so I can describe it!')


bot.polling(none_stop=True)


while True:
    time.sleep(5)

What could be the reason?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Karbivnichy, 2020-02-26
@zeni1agent

In the folder with the script, create a folder "temp".

S
Sergey Pankov, 2020-02-26
@trapwalker

  1. Move the string `temp/` to a separate variable.
  2. Check this path for existence when starting the bot.
  3. If there is no such path, you can either create it or issue an appropriate error message and exit.

If the path is without "/" at the beginning, then it is a relative path from the current one at the time the script was run.
If the bot lies along the path: `/home/my_username/my_bot_folder/my_bot.py`
And it starts like this:
`my_bot_folder/my_bot.py`
Then the current directory at the time of launch is your home:
`/home/my_username`
And the bot will try to write pictures along the path like:
`/home/my_username/temp/2345435666hashtralala.png`.
If you do not have a temp folder in your home directory, then this error will occur.
You need to create a temp folder in the right place.
You can handle this error and write `print('please make temp folder in:', os.getcwd())` in its handler

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question