標籤:UI statistic post ota python 基礎 ict key pen detail
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] += 113 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] += 118 #------------------------------------------------------------19 20 else:21 pass22 23 return file_dict24 25 26 dir_name = g.diropenbox() #這裡其實是一個擷取需要遞迴的目錄,在目錄裡找27 file_dict = StatisticeCodeLine(dir_name)28 total_line_count = 029 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 基礎實戰 -- 統計代碼量