方法一:
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]))
时间: 2024-10-19 10:30:25