[python] 字典、OS库取得当前目录的文件类型各几个

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]))

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章