1. 程式人生 > >Python 基礎實戰 -- 統計代碼量

Python 基礎實戰 -- 統計代碼量

textbox imp spl items 目前 nbsp ati extension print

 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替代

Python 基礎實戰 -- 統計代碼量