Python 統計代碼量


 1 #統計代碼量,顯示離10W行代碼還有多遠
 2 #遞歸搜索各個文件夾
 3 #顯示各個類型的源文件和源代碼數量
 4 #顯示總行數與百分比
 5 
 6 import os
 7 import easygui as g
 8 
 9 #查找文件
10 def find_file(file_path,target):
11     os.chdir(file_path)
12     all_files=os.listdir(os.curdir)
13     for each in all_files:
14         #print(each)
15         fext=os.path.splitext(each)[1]
16         if fext in target:
17             lines=calc_code(each) #統計行數
18             #print("文件%s的代碼行數是%d"%(each,lines))
19             #統計文件數
20             try:
21                 file_list[fext]+=1
22             except KeyError:
23                 file_list[fext]=1
24             #統計源代碼行數
25             try:
26                 source_list[fext] += lines
27                 #print(source_list[fext])
28             except KeyError:
29                 source_list[fext] = lines
30                 #print(source_list[fext])
31 
32 
33 
34         if os.path.isdir(each):
35             find_file(each,target) # 遞歸調用
36             os.chdir(os.pardir) #返回上層目錄
37 
38 
39 #統計行數
40 def calc_code(file_name):
41     lines=0
42     with open(file_name,encoding='gb18030',errors='ignore') as f:
43         print("正在分析文件%s..."%file_name)
44         try:
45             for eachline in f:
46                 lines += 1
47         except UnicodeDecodeError:
48             pass
49         print("文件%s分析完畢,包含代碼行%d." %(file_name,lines))
50     return lines
51 
52 
53 #顯示結果
54 def show_result(start_dir):
55     lines=0
56     total=0
57     text=''
58 
59     for i in source_list:
60         lines=source_list[i]
61         total+=lines
62         text+='%s源文件%d個,源代碼%d行\n'%(i,file_list[i],lines )
63 
64     title='統計結果'
65     msg='目前代碼行數:%d\n完成進度:%.2f%%\n距離十萬行代碼還差%d行'%(total,total/1000,100000-total)
66     g.msgbox(msg,title,text)
67 
68 #file_path=input("要查找的路徑; ")  # C:\\Users\\54353\\PycharmProjects\\untitled
69 target=['.py','.java','.c','.cc','.cpp']  #定義需要查找的源文件類型
70 file_list={}
71 source_list={}
72 g.msgbox('請打開您的文件夾','統計代碼量')
73 path=g.diropenbox('請選擇您的代碼庫:')
74 
75 find_file(path,target)
76 show_result(path)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM