|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- # ——*—— coding:cp936 -*-
- #编写一个程序,统计当前目录下每个文件类型的文件数
- import os
- all_files = os.listdir(os.curdir) #os.curdir表示当前目录
- type_dict = dict() #定义type_dict为字典
- #def file_type(all_files,type_dict,)
- for each_file in all_files:
- if os.path.isdir(each_file): #isdir(each_file)表示判断制定路径是否存在且是一个目录
- type_dict.setdefault('文件夹', 0) #设置默认值
- type_dict['文件夹'] += 1 #列表中内容项追加
- else:
- ext = os.path.splitext(each_file)[1] #splitext(path)分离文件名与扩展名,返回(f_name,f_extension)元组
- type_dict.setdefault(ext,0)
- type_dict[ext] += 1
- for each_type in type_dict.keys(): #返回字典所有的键
- #num = type_dict(each_type)
- #print('该文件夹下共有类型为【%s】的文件 %d个' % each_type, type_dict[each_type])
- print each_type
- print type_dict(each_type)
复制代码
- >>>
- .py
- Traceback (most recent call last):
- File "I:\python exercise\30lesson\0.py", line 31, in <module>
- print type_dict(each_type)
- TypeError: 'dict' object is not callable
复制代码
最后一句打印语句,不知道怎么搞啦
有可能还是跟python 2 有关
哪位帮忙解决下
python2和3的问题,是个头疼的问题啊
多谢啦 |
|