Skip to content
Advertisement

why python show file not found error during run time?

I am write a code for collage maker using python and place 2 picture in same folder in which code file exist but when I run a code this error shown: FileNotFoundError: [Errno 2] No such file or directory: ‘background.jpg’ and my line of code is:

img1 = Image.open("background.jpg")
img2 = Image.open('Awais.jpg')

My Question is how I handle this error?

Advertisement

Answer

You would be getting this error because even though you have placed the code and the image in the same directory/folder , the path from where you’re running the code matter as that’s the working directory for the program.

The best here would be to either use absolute path or make use of os.path.dirname(__file__) and other utilities to fetch the directory where the code file is and use it to reference the image paths.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement