鱼C论坛

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

[已解决]请问这段代码哪里有问题?

[复制链接]
发表于 2017-8-12 15:08:04 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 木一 于 2017-8-12 15:09 编辑

能够正常运行,没有报错,但是计算结果不对,请问哪里有问题?计算结果显示代码只有72行,明显不对,但是源文件数是对的。
  1. import easygui as g
  2. import os

  3. def show_result(start_dir):
  4.     lines = 0
  5.     total = 0
  6.     text = ''

  7.     for i in source_list:
  8.         lines = source_list[i]
  9.         total += lines
  10.         text += '%s源文件%d个,源代码%d行\n'%(i,file_list[i],lines)
  11.     title = '统计结果'
  12.     msg = '您目前累计共编写了%d行代码,离十万行还差%d行,请继续努力!'%(total,100000-total)
  13.     g.textbox(msg,title,text)

  14. def calc_code(file_name):
  15.     lines = 0
  16.     with open(file_name) as f:
  17.         print('正在分析文件:%s...'%file_name)
  18.         try:
  19.             for each_line in f:
  20.                 lines += 1
  21.         except UnicodeDecodeError:
  22.             pass
  23.     return lines

  24. def search_file(start_dir):
  25.     os.chdir(start_dir)

  26.     for each_file in os.listdir(os.curdir):
  27.         ext = os.path.splitext(each_file)[1]
  28.         if ext in target:
  29.             lines = calc_code(each_file)
  30.             try:
  31.                 file_list[ext] += 1
  32.             except KeyError:
  33.                 file_list[ext] = 1
  34.             try:
  35.                 source_list[ext] += lines
  36.             except KeyError:
  37.                 source_list[ext] = lines

  38.         if os.path.isdir(each_file):
  39.             search_file(each_file)
  40.             os.chdir(os.pardir)

  41. target = ['.c','.cpp','.py']
  42. file_list = {}
  43. source_list = {}

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

  46. search_file(path)
  47. show_result(path)
  48.    
复制代码
最佳答案
2017-8-12 21:57:01
try:
            for each_line in f:
                lines += 1
        except UnicodeDecodeError:
            pass


这个位置忽略了不能读取的文件

参考:
  1. https://pypi.python.org/pypi/chardet

  2. import chardet
  3. #以rb读取文件返回文件的编码(用到了chardet类)
  4.         with open(file_name, 'rb') as f:
  5.             raw = f.read()
  6.             result = chardet.detect(raw)  
  7.             encoding = result['encoding']

  8.         lines = 0   
  9.         with open(file_name,encoding=encoding) as f:
  10.             print('正在分析文件:%s ...' % file_name)     
  11.             try:
  12.                 for each_line in f:
  13.                     lines += 1
  14.             except Exception as reason:
  15.                 print(str(reason)) # 读取出错显示错误信息......
  16.         print('%s -> %s' % (file_name,lines))
  17.         return lines
复制代码
VX(}JBRL{TGG{T20I~C2IMK.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-8-12 21:57:01 | 显示全部楼层    本楼为最佳答案   
try:
            for each_line in f:
                lines += 1
        except UnicodeDecodeError:
            pass


这个位置忽略了不能读取的文件

参考:
  1. https://pypi.python.org/pypi/chardet

  2. import chardet
  3. #以rb读取文件返回文件的编码(用到了chardet类)
  4.         with open(file_name, 'rb') as f:
  5.             raw = f.read()
  6.             result = chardet.detect(raw)  
  7.             encoding = result['encoding']

  8.         lines = 0   
  9.         with open(file_name,encoding=encoding) as f:
  10.             print('正在分析文件:%s ...' % file_name)     
  11.             try:
  12.                 for each_line in f:
  13.                     lines += 1
  14.             except Exception as reason:
  15.                 print(str(reason)) # 读取出错显示错误信息......
  16.         print('%s -> %s' % (file_name,lines))
  17.         return lines
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 2 反对 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-1 11:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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