python - 如何找到具有目标扩展名列表的文件编号,例如:.txt、.pdf 等,并使用 python 为当前目录和子目录分别打印
问题描述
import os
def fileCount(folder):
"count the number of files in a directory"
count = 0
for filename in os.listdir(folder):
path = os.path.join(folder, filename)
if os.path.isfile(path):
count += 1
elif os.path.isdir(path):
count += fileCount(path)
print(folder,'having',count)
return count
fileCount('.')
解决方案
import glob
def countMe(MyPath,typeofFile):
numInstances = len(glob.glob1(MyPath,'*.'+typeofFile))
return numInstances
typeofFile
应该是您要查找的扩展名的字符串。如果你正在寻找.pdf
那么typeofFile = 'pdf'
推荐阅读
- java - 启动时未触发通知
- c# - 在 c# windows 应用程序表单的仪表板中登录后如何获取用户名
- c# - 需要更新拖曳实体时无法访问 asp core 3 中的已处置对象
- angular - 具有空值的 switchMap
- python - 如何使用 Django 进行表单验证?
- java - 如何在函数中的子字符串和比较器中的两个值中实现二进制搜索?
- pytorch - pytorch F.cross_entropy 不对权重应用梯度
- iis - SharePoint 2019 - 添加自定义响应标头不起作用
- javascript - 使用 render props 访问 refs
- machine-learning - 卷积神经网络中的池化不会在特征检测中失去精度吗?