user9687048
user9687048

Reputation:

Python cannot find specified folder

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)

Upvotes: 1

Views: 50

Answers (1)

Tripp Kinetics
Tripp Kinetics

Reputation: 5439

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

Upvotes: 2

Related Questions