'No such file or directory

I am trying to use Pillow Library in Python. I am writing basic code to open up a picture that I have saved.

But every time I run the code, it says

"can't open file 'C:\Users\saadn\PycharmProjects\Giraffe\main.py': 
[Errno 2] No such file or directory".

The path for where the picture is saved is "C:\Users\saadn\PycharmProjects\Giraffe\DirectoryAssign1"

Now I know that in the error it says "Girrafe\main.py". but I don't have a python file named main at all. Someone guide me.

the code I wrote was

from PIL import Image
img = Image.open("dany.jpg")
img.show() 

Here is a screenshot for better understanding.



Solution 1:[1]

Actually I think there are 2 issues, and not 1.

Error 1 - Wrong File is being tried to run :

In the picture you attached there is an error which means it cannot open main.py (No such file) . You entered a wrong file name, your file name is OpenAndShow.py

How to fix?

Be sure to open the terminal or command prompt and be sure you are in the following directory C:\Users\saadn\PycharmProjects\Giraffe\ and enter the following command as you are a Windows OS User: py OpenAndShow.py

Fun fact: Of course Python won't search in the whole PC or directory (including folders which are in the directory) , so that's why you need to mention it is in a folder in the Giraffe folder!


Error 2 - Wrong location for the picture

If you had fixed error 1, you will get another error, that dany.jpg is not such file, why ? Because it is in another folder inside the folder your code is located, you can use dany.jpg when they are in the same folder, not in another folder in the same folder, in this case, it is in DirectoryAssign1 which is in Girrafe folder.

How to fix?

To fix it, change the path:

From : The current Path: dany.jpg

To : The following path below: DirectoryAssign1\dany.jpg


Solution 2:[2]

You are running the wrong file. In PyCharm, afaik you can right click on the file and run. Your PyCharm is now trying to run the file main.py.

Otherwise, you can use the terminal:

python OpenAndShow.py

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Omar The Dev
Solution 2 Timmy Chan