鱼C论坛

 找回密码
 立即注册
查看: 1572|回复: 3

[已解决]检查了几遍不知道哪错了,望大神指点

[复制链接]
发表于 2017-11-4 23:30:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这是小甲鱼的标准答案
  1. import os

  2. all_files = os.listdir(os.curdir) # 使用os.curdir表示当前目录更标准
  3. type_dict = dict()

  4. for each_file in all_files:
  5.     if os.path.isdir(each_file):
  6.         type_dict.setdefault('文件夹', 0)
  7.         type_dict['文件夹'] += 1
  8.     else:
  9.         ext = os.path.splitext(each_file)[1]
  10.         type_dict.setdefault(ext, 0)
  11.         type_dict[ext] += 1

  12. for each_type in type_dict.keys():
  13.     print('该文件夹下共有类型为【%s】的文件 %d 个' % (each_type, type_dict[each_type]))
复制代码


这是我的(有错)

  1. import os

  2. all_files = os.listdir(os.curdir)
  3. type_dict = {}


  4. for each_files in all_files:
  5.     if os.path.isdir(each_file):
  6.         type_dict.setdefault('文件名',0)
  7.         type_dict['文件名'] += 1
  8.     else:
  9.         exten = os.path.splitext(each_files)[1]
  10.         type_dict.setdefault(exten,0)
  11.         type_dict[exten] += 1


  12. for each_type in type_dict.keys():
  13.     print('该%s类型有 %d个 '%(each_type,type_dict[each_type])
复制代码




最佳答案
2017-11-5 00:44:23
本帖最后由 Amazon 于 2017-11-5 00:45 编辑

第 8 行   if os.path.isdir(each_file):

  1. for each_files in all_files:
  2.     if os.path.isdir(each_file):  
复制代码


each_file 应该是 each_files,因为你第 7 行写的 for each_files in all_files:

最后一行print('该%s类型有 %d个 '%(each_type,type_dict[each_type])

  1.     print('该%s类型有 %d个 '%(each_type,type_dict[each_type])
复制代码


括号没有闭合,正确代码是:

  1.     print('该%s类型有 %d个 '%(each_type,type_dict[each_type]))
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-11-5 00:44:23 | 显示全部楼层    本楼为最佳答案   
本帖最后由 Amazon 于 2017-11-5 00:45 编辑

第 8 行   if os.path.isdir(each_file):

  1. for each_files in all_files:
  2.     if os.path.isdir(each_file):  
复制代码


each_file 应该是 each_files,因为你第 7 行写的 for each_files in all_files:

最后一行print('该%s类型有 %d个 '%(each_type,type_dict[each_type])

  1.     print('该%s类型有 %d个 '%(each_type,type_dict[each_type])
复制代码


括号没有闭合,正确代码是:

  1.     print('该%s类型有 %d个 '%(each_type,type_dict[each_type]))
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-11-5 08:39:18 | 显示全部楼层
  1. import os

  2. all_files = os.listdir(os.curdir)
  3. type_dict = {}


  4. for each_files in all_files:
  5.     if os.path.isdir(each_files):
  6.         type_dict.setdefault('文件名',0)
  7.         type_dict['文件名'] += 1
  8.     else:
  9.         exten = os.path.splitext(each_files)[1]
  10.         type_dict.setdefault(exten,0)
  11.         type_dict[exten] += 1


  12. for each_type in type_dict.keys():
  13.     print('该%s类型有 %d个 '%((each_type,type_dict[each_type])
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-11-5 10:48:11 | 显示全部楼层
Amazon 发表于 2017-11-5 00:44
第 8 行   if os.path.isdir(each_file):

谢谢,现在对了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-3-3 17:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表