import os
all_files = os.listdir(os.curdir)
type_dict = dict()
for each_file in all_files:
if os.path.isdir(each_file):
type_dict.setdefault('文件夹', 0)
type_dict['文件夹'] += 1
else:
ext = os.path.splitext(each_file)[1]
type_dict.setdefault(ext, 0)
type_dict[ext] +=1
for each_type in type_dict.keys():
print('该文件夹下共为类型为%s得文件%d个' % (each_type, type_dict[each_type]))[python] 字典、OS库取得当前目录的文件类型各几个
最新推荐文章于 2022-01-17 10:44:55 发布
本篇博客介绍了一种使用Python os模块来统计当前目录下各种文件类型的数量的方法。通过遍历当前目录的所有条目并检查每个条目的类型(文件或文件夹),然后将其扩展名或类别计入相应的计数器。
1260

被折叠的 条评论
为什么被折叠?



