|
发表于 2021-7-30 19:28:40
|
显示全部楼层
这是我以前写的,你先看看,我去回顾下
- import os
- import easygui as eg
- def mq(path, code_count, count_type, c_type):
- os.chdir(path)
- for eachfile in os.listdir(os.getcwd()):
- if os.path.isdir(eachfile):
- mq(eachfile, code_count, count_type, c_type)
- os.chdir(os.pardir)
- else:
- (f_name, f_extension) = os.path.splitext(eachfile)
- if f_extension in count_type:
- count = 0
- with open(eachfile) as fp:
- try:
- for each_line in fp:
- count += 1
- except UnicodeDecodeError:
- pass
- if f_extension in code_count.keys():
- code_count[f_extension] += count
- c_type[f_extension] += 1
- else:
- code_count.setdefault(f_extension, count)
- c_type.setdefault(f_extension, 1)
- eg.msgbox('欢迎使用大马强代码统计软件', '\@.@/', ok_button='继续')
- Dir = eg.diropenbox(title='请选择要统计的目录')
- eg.msgbox('正在统计代码行数...\n请等待10分钟', '提示', ok_button='妈耶!GKD!')
- code_count = dict()
- c_type = dict()
- count_type = ['.py', '.c', '.cpp']
- mq(Dir, code_count, count_type, c_type)
- total = 0 # 改进函数列表推导式
- for each in list(code_count.keys()):
- total += code_count[each]
- with open('统计.txt', 'w', encoding='utf-8') as fp:
- for j in list(code_count.keys()):
- fp.write('【%s】源文件%d个,源代码%d\n' % (j, c_type[j], code_count[j]))
- with open('统计.txt', encoding='utf-8') as fp:
- msg = '您当前已经积累了%d行代码,完成进度:%.2f%% \n离10w行代码还有%d行,请继续努力!' % (
- total, total/1000, 100000-total) # 百分数表示【%%】
- eg.textbox(msg, '统计结果', text=fp.read())
复制代码 |
|