python30讲课后习题
我应该怎么修改代码才能正确输出import os
count = 0
dic = {}
list1 = []
a = os.listdir('F:\\python作业库\\文件')
for first in a:
secornd = os.path.splitext(first)
list1.append(secornd) #列表1用于收集所有扩展名
for each_name in list1: #统计每个扩展名出现的次数
if each_name not in dic:
dic = 1
else:
dic += 1
print(dic)
for k in dic.keys():
for v in dic.values():
print('该文件夹下共有类型为%s的文件%d个'%(k,v))
改成
import os
count = 0
dic = {}
list1 = []
a = os.listdir('F:\\python作业库\\文件')
for first in a:
secornd = os.path.splitext(first)
list1.append(secornd) #列表1用于收集所有扩展名
for each_name in list1: #统计每个扩展名出现的次数
if each_name not in dic:
dic = 1
else:
dic += 1
print(dic)
i = 0
for k in dic.keys():
for v in dic.values():
print('该文件夹下共有类型为%s的文件%d个'%(k,v))
i+=1
更好的办法
import os
count = 0
dic = {}
list1 = []
a = os.listdir('F:\\python作业库\\文件')
for first in a:
second = os.path.splitext(first)
list1.append(second) #列表1用于收集所有扩展名
for each_name in list1: #统计每个扩展名出现的次数
if each_name not in dic:
dic = 1
else:
dic += 1
print(dic)
for k, v in dic.items():
print('该文件夹下共有类型为%s的文件%d个'%(k,v))
ckblt 发表于 2022-1-25 21:43
改成
报错啊兄弟
页:
[1]