简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM