|
|
3鱼币
- 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) :
- ext = os.path.splitext(each_file)[1]
- if ext in target :
- lines = calc_code(each_file)
-
- try:
- file_list[ext] += 1
- except KeyError:
- file_list[ext] = 1
- print(file_list)#这里键对应的值是递增的
- # 统计源代码行数
- try:
- source_list[ext] += lines
- except KeyError:
- source_list[ext] = lines
- print(source_list)#这里每一个键对应的值都是行数,为什么不是递增的?
- if os.path.isdir(each_file) :
- search_file(each_file)
- os.chdir(os.pardir)
-
- target = ['.c', '.cpp', '.py', '.cc', '.java', '.pas', '.asm']
- file_list = {}
- source_list = {}
- g.msgbox("请打开您存放所有代码的文件夹......", "统计代码量")
- path = g.diropenbox("请选择您的代码库:")
- search_file(path)
- show_result(path)
复制代码
为什么file_list这个字典里的键对应的值是递增的,source_list这个字典里对应的值不是递增的? |
最佳答案
查看完整内容
我知道 我知道! 首先 第一个递增是因为语句
try:
file_list[ext] += 1
所以导致 file_list【a】的值递增
而后面的source_list 它是+lines 而lines = calc_code(each_file) 他是通过调用上面的函数calc_code得到的。calc_code函数中 统计文件中有多少行代码, 你总不能所有的文件都是相同行数的代码吧,所以导致不是递增的。
|