|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
# 4. 写一个程序统计你当前代码量的总和,并显示离十万行代码量还有多远?
#
# 要求一:递归搜索各个文件夹
# 要求二:显示各个类型的源文件和源代码数量(先用字典简单显示)
# 要求三:显示总行数与百分比 (还没做到)
#coding=utf-8
import os
import easygui as g
temp_set = set()
temp_list = []
memory_dict = dict()
sum_line = 0
def count_code():
global sum_line
global memory_dict
path = g.diropenbox()
os.chdir(path)
for each_file in os.listdir(os.curdir):
temp_set.add(os.path.splitext(each_file)[1])
# temp_set = ['.txt', '.py']
for dif_type in temp_set:
sum_line = 0
if os.path.splitext(each_file)[1] == dif_type:
try:
with open(str(each_file), 'r', encoding='utf-8') as f:
f_list = list(f)
sum_line = len(f_list)
temp_list.append(sum_line) #应该是问题所在?T_T
memory_dict[dif_type] = temp_list
except UnicodeDecodeError:
pass
if os.path.isdir(each_file):
count_code(each_file)
os.chdir(os.pardir)
print(memory_dict)
count_code()
结果出来之后,发现行数会重复计算,想了好久没想到解决办法,有大佬可以指点一下吗?
结果:{'.py': [18, 95, 41, 11, 42, 19, 9, 23, 44, 21, 18, 9, 33, 40, 31, 31, 56], '.txt': [18, 95, 41, 11, 42, 19, 9, 23, 44, 21, 18, 9, 33, 40, 31, 31, 56]}
楼主 你已经知道问题所在了呀,我看那里就是问题,所以我给改了一下,把你注释问题所在的那行改成:
temp_list = memory_dict.get(dif_type, 0) + sum_line
|
|