FileNotFoundError: [Errno 2] No such file or directory for pictures on Python 3.7

15,615

If the image file "v" is in the same folder as your python code, your code should load it. The error shows it is missing the file. I would recommend checking for file extensions (maybe file name is "v.jpg").
And if your file is not in the python folder and is in a different folder, the easiest way is to include the full address to your image file:

image=Image.open("full_path_to_file/"+"v")

The same approach with better practice would be like this:

import os
image=Image.open(os.path.join("full_path_to_file", "v"))

You can also use the relational address to file. "../" will take you to one directory above your current directory. For example, if your file directory structure is:

  • directory
    • |
    • v.jpg
    • subdirectory
      • |
      • your_python_code.py

You can use:

image=Image.open("../v")
Share:
15,615
Lorette VIGIER
Author by

Lorette VIGIER

Updated on July 22, 2022

Comments

  • Lorette VIGIER
    Lorette VIGIER almost 2 years

    I am a beginner on Python. I try to open a picture to print its size and format. My picture is in several folders:

    • my images
    • the folder of my Python codes
    • the folder of anaconda3\lib\site-packages\PIL

    I have already wrote only the name of the picture, then the exact path but nothing works (my picture is in .jpg).

    Here is my code :

    from PIL import Image
    
    image = Image.open("v")    
    print("nbr of columns and lines", image.size, image.format)
    data = list(image.getdata())
    print(data[:30]) 
    

    and my error message:

    runfile('C:/Users/Optimal Conseil/.spyder-py3/temp.py', wdir='C:/Users/Optimal Conseil/.spyder-py3') Traceback (most recent call last):

    File "C:\Users\Optimal Conseil.spyder-py3\temp.py", line 4, in image=Image.open("v")

    File "C:\Users\Optimal Conseil\anaconda3\lib\site-packages\PIL\Image.py", line 2809, in open fp = builtins.open(filename, "rb")

    FileNotFoundError: [Errno 2] No such file or directory: 'v'

    • Nandu Raj
      Nandu Raj about 4 years
      There is no file named "v". You should give the full file name, if the file is in the same folder as that of this code: then image=Image.open("v.jpg") or give the full path
  • Lorette VIGIER
    Lorette VIGIER about 4 years
    Thank you so much, it worked with import os and I added a r at the start of the parenthesis.
  • Ehsan
    Ehsan about 4 years
    Great. If the answer is correct, would you mind accepting it to help others identify it? r'..' interprets the string in raw form (interpret any character literally) which means you might have had special characters in your path address that needed to be interpreted literally.