M
M
Maxim2020-08-08 21:17:16
Python
Maxim, 2020-08-08 21:17:16

Python. Why FileNotFoundError: [Errno 2] No such file or directory: 'screenshot.PNG'?

Hello.

Please help me fix the error. I have a script https://github.com/yatakoi/python-scripts/blob/mas... , which, when launched, refers to the email specified in config.ini and if there are new letters, it downloads them to itself, and then sends me in Telegram.

Everything would be fine, but if there is an attachment in the letter, then the script swears at

Traceback (most recent call last):
  File "/home/m.kos/send_tickets/main.py", line 140, in <module>
    send_emails_telegram(bot, chat)
  File "/home/m.kos/send_tickets/main.py", line 101, in send_emails_telegram
    bot.send_document(chat_id, open(file, "rb"))
FileNotFoundError: [Errno 2] No such file or directory: 'screenshot.PNG'

And at this time:
[[email protected] ~]$ ll
total 60
-rw-r--r-- 1 m.kos m.kos   192 Aug  8 09:37 config.ini
drwxrwxr-x 4 m.kos m.kos   174 Aug  8 20:55 INBOX
-rw-r--r-- 1 m.kos m.kos  3976 Aug  8 20:55 main.py.log

[[email protected] ~]$ ll INBOX/
total 0
drwxrwxr-x 3 m.kos m.kos 25 Aug  8 19:55 Проверочный аттач от MAX [email protected]
[[email protected] ~]$
[[email protected] ~]$ ll INBOX/\Проверочный\ аттач\ от\ MAX\ ya\@ya.ru/
total 0
drwxrwxr-x 2 m.kos m.kos 28 Aug  8 19:55 attachments
[[email protected] ~]$ ll INBOX/\Проверочный\ аттач\ от\ MAX\ ya\@ya.ru/attachments/
total 164
-rw-rw-r-- 1 m.kos m.kos 164559 Aug  8 19:55 screenshot.PNG

That is, the file exists. I would be grateful if you tell me what is wrong in line 101.

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
D
Dr. Bacon, 2020-08-08
@bacon

(Tiredly) classic independent of the PL. The question is, in which directory is the script running? 'screenshot.PNG' is a file in the directory where the script is run from. And you need to do it right - calculate the absolute path to this file and pass it to open, then it will not care about this problem.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question