Karnivaurus
Karnivaurus

Reputation: 24121

Counting number of images in a directory: not working

I am running Python 2.7.6 on Windows 7. I have a directory at C:/Data/Images which contains a number of JPEG images. So, I navigate to C:/Data in Command Prompt, and then run the Python shell by entering python. Then, I enter the following:

print len([f for f in os.listdir('Images') if os.path.isfile(f)])

And this returns 0, even though I know that there are several images in C:/Data/Images.

Why is this?

Upvotes: 1

Views: 1297

Answers (1)

falsetru
falsetru

Reputation: 369064

os.listdir returns a list of filenames (wihtout directory path part). You need to join the directory name with filename.

[f for f in os.listdir('Images') if os.path.isfile(os.path.join('Images', f))]

Upvotes: 2

Related Questions