首页 > 其他 > 详细

编程统计制定路径的文件格式

时间:2017-02-12 01:05:20      阅读:217      评论:0      收藏:0      [点我收藏+]

方法一:

 1 #编程统计制定路径的文件格式
 2 import os
 3 format_file =dict()
 4 count=0
 5 for each in os.listdir(E:\\):
 6     count+=1
 7     (former, latter) = os.path.splitext(each)
 8     if latter not in format_file:
 9         format_file[latter]=1
10     else:
11         format_file[latter]+= 1    
12 print(该文件夹下共有%d个文件‘% count)
13 for eachkey in format_file.keys():  
14     print(该文件夹共有类型为【%s】的文件%d个‘ % (eachkey, format_file[eachkey]))


方法二:

 1 import os
 2 
 3 all_files = os.listdir(E:\\) # 使用os.curdir表示当前目录更标准
 4 type_dict = dict()
 5 
 6 for each_file in all_files:
 7     if os.path.isdir(each_file):
 8         type_dict.setdefault(文件夹, 0)
 9         type_dict[文件夹] += 1
10     else:
11         ext = os.path.splitext(each_file)[1]
12         type_dict.setdefault(ext, 0)
13         type_dict[ext] += 1
14 
15 for each_type in type_dict.keys():
16     print(该文件夹下共有类型为【%s】的文件 %d 个 % (each_type, type_dict[each_type]))

 

编程统计制定路径的文件格式

原文:http://www.cnblogs.com/themost/p/6390274.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!