2

My problem is I want to make a list of how many files with a certain extension or names in a folder when there are a lot of different types of files in it.

For instance, let's say there are a bunch of different types of files in a folder and I only want to count how many jpg files there are.

I tried the code below from some other person's Q&A on stackoverflow and it does show all the names of jpg files in the folder but doesn't show the number of how many jpg files there are.

import glob, os
filelist = os.listdir('D:\Train')
for file in filelist:
    if(file.endswith('jpg')):
        print(file)

Also, I'd like to know if there's a way I can count the file with certain words in the names. ex) count all the jpg files in the folder that contain 'fire' in their names (fire01.jpg, fire02.jpg, and so on)

1

4 Answers 4

8

You can use glob like this example

import glob, os
filelist = glob.glob('D:\Train\*.jpg')
print(len(filelist))
for file in filelist:
    print(file)
Sign up to request clarification or add additional context in comments.

Comments

4

Of course, as indicated in the earlier answer, len(glob.glob(...)) will tell you how many files matched the glob if you can rearticulate your requirement into a single wildcard pattern.

In the general case (for example, if you want to match .png, .jpeg, .JPEG, etc), just increment a variable each time you see an occurrence.

# Stylistic fix: don't combine imports
import glob
import os

# This is our counter
count = 0

# Notice r'' raw string to preserve literal backslash
for file in os.listdir(r'D:\Train'):
    if(file.endswith('jpg')):
        print(file)
        count += 1

print('Total:', count)

You can add a second variable fire_count too; maybe then also rename the original variable for consistency.

total_count = 0
fire_count = 0

for file in os.listdir(r'D:\Train'):
    if(file.endswith('jpg')):
        print(file)
        total_count += 1
        if 'fire' in file:
            fire_count += 1

print('Total:', total_count)
print('Fire files:', fire_count)

Comments

0

SO, I have three files, file1.pdf, file2.pdf, and file3.pdf in my path /tmp/test-dir

You can use below code to get total count of pdf files.

import glob
path_to_check = "/tmp/test-dir/"
total_txt_files = len(glob.glob1(path_to_check,"*.pdf"))
print(total_txt_files)

Output

3

Comments

0

It can be done with the os.listdir()-function, which returns a list of all filenames in a given directory, like in this example:

import os
print(len(os.listdir('D:\\Train\\'))

Since you are only interested in files that end in images, you can add a comprehension list and only keep files that end with '.jpg'

print(len([x for x in os.listdir('D:\\Train\\') if x[-4:] == '.jpg']))

1 Comment

print(len([x for x in os.listdir(PATH_SCREEN) if x[-4:] == '.jpg']))

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.