Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
Traceback (most recent call last):
  File "resize_image.py", line 6, in <module>
    img = Image.open(file)
  File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 2258, in open
    fp = builtins.open(filename, "rb")
IOError: [Errno 2] No such file or directory: '6.jpg'

I made sure that 6.jpg is available. And, it seems that I get such error for any image in this location.

How can I fix the issue?

Thanks.

Make sure it's available again, because I can tell you right now that it isn't. Fix the issue by making absolutely sure that the file is there. Check working directories, absolute paths, and so on. Python didn't suddenly decide to lie to you and cover your file with a blanket to pretend it's not there. – TigerhawkT3 May 16, 2016 at 19:14 Thanks for your comment. I really made sure that the image is available. I removed the image, and got the same error for the next image in the folder – Simplicity May 16, 2016 at 19:19 That's not what I meant at all. It's not like one file will be wearing a camouflage suit but the rest are easily visible, so removing that first one and still getting a problem means that Python is lying to you. As I said, check what directory you think Python is looking in, and then check what directory it is actually looking in. Dan's answer below points out that you're using a relative path when the files are in another folder level. – TigerhawkT3 May 16, 2016 at 19:27

The file names from os.listdir are relative to the directory given. They must be made complete by joining the dirname to their basename.

files = os.listdir('my_folder')
for file in files:
    img = Image.open(os.path.join('my_folder', file))
                There's no need to use join for the absolute path; have a look at the join method documentation.
– natka_m
                Nov 25, 2020 at 22:53
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.