鱼C论坛

 找回密码
 立即注册
查看: 547|回复: 1

《零基础入门学习python》课后作业第035讲,动动手4。文件行数统计问题。

[复制链接]
发表于 2018-12-19 19:41:57 | 显示全部楼层 |阅读模式

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

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

x
两个代码统计的行数不一样的原因是什么?


  1. def line_count(path):
  2.     """统计单一文件内容的行数,空行不计入
  3. path为文件路径"""

  4.     with open(path, 'r', encoding='UTF-8') as f:
  5.         content = list(f)
  6.         count = len(content)
  7.         return count
复制代码



  1. def calc_code(file_name):
  2.     lines = 0
  3.     with open(file_name) as f:
  4.         print('正在分析文件:%s ...' % file_name)
  5.         try:
  6.             for each_line in f:
  7.                 lines += 1
  8.         except UnicodeDecodeError:
  9.             pass # 不可避免会遇到格式不兼容的文件,这里忽略掉......
  10.     return lines
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-12-19 21:33:06 | 显示全部楼层
参考下面代码。注意看注释:
  1. import easygui as g
  2. import os, cchardet


  3. def get_file_code(path):
  4.     with open(path,'rb') as f:
  5.             msg=f.read()

  6.     enc = cchardet.detect(msg)
  7.     enc = enc['encoding']
  8.     return enc

  9. def show_result(start_dir):
  10.     lines = 0
  11.     total = 0
  12.     text = ""
  13.     for i in source_list:
  14.         lines = source_list[i]
  15.         total += lines
  16.         text += "【%s】源文件 %d 个,源代码 %d 行\n" % (i, file_list[i], lines)
  17.     title = '统计结果'
  18.     msg = '您目前共累积编写了 %d 行代码,完成进度:%.2f %%\n离 10 万行代码还差 %d 行,请继续努力!' % (total, total/1000, 100000-total)
  19.     g.textbox(msg, title, text)

  20. def calc_code(file_name):
  21.     lines = 0
  22.     enc = get_file_code(file_name) # 获取文件编码
  23.     with open(file_name, "r", encoding=enc) as f: # 以指定编码打开文件
  24.         print('正在分析文件:%s ...' % file_name)
  25.         try:
  26.             for each_line in f:
  27.                 lines += 1
  28.         except UnicodeDecodeError:
  29.             pass # 不可避免会遇到格式不兼容的文件,这里忽略掉......
  30.         
  31.     return lines

  32. def search_file(start_dir) :
  33.     #首先通过异常判断该文件夹是否有访问权限,这里用 os.listdir列目录尝试。
  34.     try:        
  35.         os.listdir(start_dir)
  36.     except:            
  37.         return
  38.    
  39.     os.chdir(start_dir)
  40.    
  41.     for each_file in os.listdir(os.curdir) :
  42.         ext = os.path.splitext(each_file)[1]
  43.         if ext in target :
  44.             lines = calc_code(each_file) # 统计行数
  45.             # 还记得异常的用法吗?如果字典中不存,抛出 KeyError,则添加字典键
  46.             # 统计文件数
  47.             try:
  48.                 file_list[ext] += 1
  49.             except KeyError:
  50.                 file_list[ext] = 1
  51.             # 统计源代码行数
  52.             try:
  53.                 source_list[ext] += lines
  54.             except KeyError:
  55.                 source_list[ext] = lines
  56.             
  57.         if os.path.isdir(each_file):
  58.             search_file(each_file) # 递归调用               
  59.             os.chdir(os.pardir) # 递归调用后切记返回上一层目录
  60.             
  61. target = ['.c', '.cpp', '.py', '.cc', '.java', '.pas', '.asm']
  62. file_list = {}
  63. source_list = {}

  64. g.msgbox("请打开您存放所有代码的文件夹......", "统计代码量")
  65. path = g.diropenbox("请选择您的代码库:")

  66. search_file(path)
  67. show_result(path)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-10-2 03:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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