|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import easygui as g
- import os
- def show_result(start_dir):
- lines = 0
- total = 0
- text =''
- for i in source_list:
- lines = source_list[i]
- total += lines
- text += '【%s】源文件 %d 个,源代码 %d 行\n' % (i,file_list[i],lines)
- title = '统计结果'
- msg = '您目前共累积编写了 %d 行代码,完成进度:%.2f %%\n离 10 万行代码还差 %d 行,请继续努力' % (total,total/1000,100000-total)
- g.textbox(msg,title,text)
- def calc_code(file_name):
- lines = 0
- with open(file_name) as f:
- print('正在分析文件: %s ...' % file_name)
- try:
- for each_line in f :
- lines += 1
- except UnicodeDecodeError:
- pass
- return lines
- def search_file(start_dir):
- os.chdir(start_dir) #改变工作目录
- for each_file in os.listdir(os.curdir): #os.curdir当前目录,os.listdir列举目录中文件名
- ext = os.path.splitext(each_file)[1] #分离文件与扩展名,ext储存扩展名
- if ext in target:
- lines = calc_code(each_file) #统计行数
- #统计文件数
- try:
- file_list[ext] += 1
- except KeyError:
- file_list[ext] = 1
- #统计源代码行数
- try:
- source_list[ext]+= lines
- except KeyError:
- source_list[ext] = lines
- if os.path.isdir(each_file): #判断是否存在目录os.path.isdir
- search_file(each_file) #递归调用
- os.chdir(os.pardir)#递归调用后切记返回上一层目录,os.pardir指代上级目录
- target = ['.c','.ccp','.py',',cc','.java','.pas','.asm']
- file_list ={ }
- source_list = { }
- g.msgbox('请打开您存放所有代码的文件夹。。。','统计代码量')
- path = g.diropenbox('请选择您的代码库:')
- search_file(path)
- show_result(path)
-
复制代码
这个题我自己统计出来的结果显示源代码行数为0,这是为什么?
我个人感觉是因为cacl模块中的except UnicodeDecodeError 这里的本意是无视那些无法识别的编码的文件,但是你的代码文件也是用gbk编码的,导致他直接无视了,所以显示代码是0行。
|
|