|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import os
- #all_files = os.listdir('C:\\Users\\asus\\Desktop\\python练习') # 使用os.curdir表示当前目录更标准
- all_file = os.listdir(os.curdir)
- type_dict = dict()
- for each_file in all_files:
- if os.path.isdir(each_file): #判断是否是文件夹
- type_dict.setdefault('文件夹', 0)
- type_dict['文件夹'] += 1
- else:
- ext = os.path.splitext(each_file)[1]
- type_dict.setdefault(ext, 0)
- type_dict[ext] += 1
- for each_type in type_dict.keys():
- print('该文件夹下共有类型为【%s】的文件 %d 个' % (each_type, type_dict[each_type]))
复制代码
运行结果:runfile('C:/Users/asus/Desktop/小甲鱼/习题/030/030-2-0-2.py', wdir='C:/Users/asus/Desktop/小甲鱼/习题/030')
该文件夹下共有类型为【】的文件 19 个
该文件夹下共有类型为【.docx】的文件 4 个
该文件夹下共有类型为【.xlsx】的文件 1 个
文件夹这个键就没有输出,求问是怎么回事? |
|