|
发表于 2020-1-2 15:41:27
|
显示全部楼层
- import os
- path = r'C:\Users\wb-fxy647636\Desktop\study'
- all_files = os.listdir(path)
- # print(all_files)
- # type_dict.clear()
- type_dict = dict()
- for each_file in all_files:
- if os.path.isdir(os.path.join(path, each_file)): # 提供完整路径
- type_dict.setdefault('文件夹',0)
- type_dict['文件夹'] += 1
- # print(each_file)
- # print(type_dict)
- 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]))
复制代码 |
|