|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我编写完的代码,为啥不能显示文件夹的统计个数?
其他功能都正常
- import os
- def change(all_file):
- fun = {} #用字典其值可以直接存储数量,因而用字典
- for each in all_file:
- if os.path.isdir(each): #指定路径是一个目录,证明是个文件夹
- fun.setdefault('文件夹',0)
- fun['文件夹'] +=1
- else:
- (name,exp) = os.path.splitext(each)
- fun.setdefault(exp,0)
- fun[exp] += 1
- print(fun)
- for i in fun.keys():
- print('该文件下共有类型为【%s】的文件%d个'%(i,fun[i]))
- all_file = os.listdir(r'C:\Users\Administrator\Desktop\测试文件夹')
- change(all_file)
复制代码
我的文件夹统计个数显示成了【】2个,小甲鱼的是【‘文件夹’】2个
很疑惑,求解,显示结果如图
本帖最后由 Twilight6 于 2020-6-14 17:11 编辑
你上面的 if 判断的是文件 而不是目录路径
导致就算是文件夹也是执行else 而不是 if
(name,exp) = os.path.splitext(each)
这边的切割对文件夹无效,所以你
fun.setdefault(exp,0)
会弄个空字符串的键到字典中去
|
-
这是小甲鱼的 显示
-
这是我的显示
|