1

I have a folder within my Python project folder that contains around 400 images. I'd like to be able to write a Python script that can access the folder and then pull all of the filenames and print them to the console.

I have the below code, but when I run it I get the error message FileNotFoundError: [WinError 2] The system cannot find the file specified: '/images/'

What do I need to do to fix this? The folder name and os.chdir() parameters are correct.

import glob, os
os.chdir("/images/")
for file in glob.glob("*.png"):
    print(file)

1 Answer 1

2

You shouldn't have the leading slash. That indicates you want a directory images in the root directory. Change "/images/" to "images/".

Sign up to request clarification or add additional context in comments.

2 Comments

@Ramesh Yes. If your images directory were one level above your project.
@TrippKinetics Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.