我如何找到目标扩展名列表中的文件数量,如.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'

最新更新