1 import os 2 import easygui as g 3 4 def StatisticeCodeLine(dir_name): 5 file_dict = {".py":[0,0],".c":[0,0],".cpp":[0,0],".pas":[0,0],".asm":[0,0]} 6 all_child_dir = os.walk(dir_name) 7 8 for item in all_child_dir: 9 for file in item[2]: 10 file_name,file_extension = os.path.splitext(file) 11 if file_extension in file_dict: 12 file_dict[file_extension][0] += 1 13 14 #------------------------------------------------------------ 15 with open(os.path.join(item[0],file),"r",encoding=‘utf-8‘) as f: 16 for i in f: 17 file_dict[file_extension][1] += 1 18 #------------------------------------------------------------ 19 20 else: 21 pass 22 23 return file_dict 24 25 26 dir_name = g.diropenbox() #这里其实是一个获取需要递归的目录,在目录里找 27 file_dict = StatisticeCodeLine(dir_name) 28 total_line_count = 0 29 detail = "" 30 for key,value in file_dict.items(): 31 total_line_count += value[1] 32 detail += str(key) + "源文件" + str(value[0]) + "个,源代码" + str(value[1])+"行\n" 33 34 total_line_count = "您目前累积编写了{0}行代码,完成度{1}%\n离10万行代码还差{2}行,请继续努力!".format(total_line_count,total_line_count / 1000,100000-total_line_count) 35 36 g.textbox(total_line_count,"统计结果",text=detail) #其实你想的话这里也可以用print替代