Python statistics data set file number

 1. Python counts the number of files in each sub-file under a folder

# 111 Python统计文件夹下各个子文件中的文件个数
import os
file = '路径'
for dirpath, dirnames, filenames in os.walk(file):
    file_count = 0
    for filename in filenames:
        file_count = file_count + 1
    print(dirpath,file_count) #dirpath子目录路径,file_count子目录中的文件个数

2. Count the number of all jpg images in the current directory (replace according to the relevant format of the statistical data)

# 222 统计当前目录下面所有的jpg图片的数量
import os
count=0
for root,dirs,files in os.walk("./"):
    for file in files:
        ext=os.path.splitext(file)[-1].lower()
        if ext=='.jpg':
            count=count+1
print(count)

3. Count the number of jpg files and png images in the directory respectively

# 333 分别统计目录中jpg文件和png图片的数量
import os
ret={"jpg":0,"png":0}
for root,dirs,files in os.walk("./"):
    for file in files:
        ext=os.path.splitext(file)[-1].lower()
        if ext=='.jpg':
            ret["jpg"]=ret["jpg"]+1
        if ext==".png":
            ret["png"]=ret["png"]+1

Guess you like

Origin blog.csdn.net/haimengjie/article/details/128763544