1. Python统计文件夹下各个子文件中的文件个数
# 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. 统计当前目录下面所有的jpg图片的数量(按照统计数据的相关格式自行替换)
# 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. 分别统计目录中jpg文件和png图片的数量
# 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