简体   繁体   English

(Python) - 如何计算具有特定扩展名或名称的文件中的文件数

[英](Python) - How to count number of files in a file with certain extension or name

I'm a newbie in programming and I was wondering if you could help me out.我是编程新手,我想知道您是否可以帮助我。

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.例如,假设一个文件夹中有一堆不同类型的文件,我只想计算有多少个 jpg 文件。

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.我在 stackoverflow 上从其他人的问答中尝试了下面的代码,它确实显示了文件夹中所有 jpg 文件的名称,但没有显示有多少 jpg 文件的数量。

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)例如)计算文件夹中名称中包含“火”的所有 jpg 文件(fire01.jpg、fire02.jpg 等)

Thanks a lot!非常感谢!

You can use glob like this example你可以像这个例子一样使用 glob

import glob, os
filelist = glob.glob('D:\Train\*.jpg')
print(len(filelist))
for file in filelist:
    print(file)

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.当然,如前面的答案所示, len(glob.glob(...))会告诉您有多少文件与 glob 匹配,如果您可以将您的要求重新表达为单个通配符模式。

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.在一般情况下(例如,如果您想匹配.png.jpeg.JPEG等),只需在每次看到出现时增加一个变量。

# 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;您也可以添加第二个变量fire_count 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)

SO, I have three files, file1.pdf, file2.pdf, and file3.pdf in my path /tmp/test-dir所以,我的路径 /tmp/test-dir 中有三个文件,file1.pdf、file2.pdf 和 file3.pdf

You can use below code to get total count of pdf files.您可以使用以下代码获取 pdf 文件的总数。

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

Output输出

3 3

It can be done with the os.listdir()-function, which returns a list of all filenames in a given directory, like in this example:它可以通过 os.listdir() 函数完成,该函数返回给定目录中所有文件名的列表,如下例所示:

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'由于您只对以图像结尾的文件感兴趣,您可以添加一个理解列表并只保留以“.jpg”结尾的文件

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM