Python實現(xiàn)代碼統(tǒng)計工具
本文實例為大家分享了Python實現(xiàn)代碼統(tǒng)計工具的具體代碼,供大家參考,具體內(nèi)容如下
思路:首先獲取所有文件,然后統(tǒng)計每個文件中代碼的行數(shù),最后將行數(shù)相加.
實現(xiàn)的功能:
統(tǒng)計每個文件的行數(shù);
統(tǒng)計總行數(shù);
支持指定統(tǒng)計文件類型,排除不想統(tǒng)計的文件類型;
排除空行;
排除注釋行
import os import sys import os.path #for i in sys.argv: # print (i) # 判斷單個文件的代碼行數(shù) def count_file_lines(file_path): line_count = 0 flag=True try: fp = open(file_path,"r",encoding="utf-8") encoding_type="utf-8" fp.close() except: encoding_type="gbk" with open(file_path,"r",encoding=encoding_type) as fp: for line in fp: #print (line_count) if line.strip()=="": continue else: if line.strip().endswith("'''") and flag == False: flag=True continue if line.strip().endswith('"""') and flag == False: flag=True continue if flag == False: continue if line.strip().startswith("#encoding") or line.strip().startswith("#-*-"): line_count += 1 #elif line.strip().startswith('"""') and line.strip().endswith('"""') and line.strip()!='"""': #continue #elif line.strip().startswith("'''") and line.strip().endswith("'''") and line.strip()!="'''": #continue elif line.strip().startswith('#'): continue elif line.strip().startswith("'''") and flag == True: flag = False continue elif line.strip().startswith('"""') and flag == True: flag = False continue else: line_count += 1 return line_count def count_code_lines(path,file_types=[]): # 判斷路徑是否存在 if not os.path.exists(path): print("您輸入的目錄或文件路徑不存在") return 0 line_count=0 #代碼行總數(shù) file_lines_dict={} #每個文件代碼行數(shù) # 判斷是否為文件 if os.path.isfile(path): file_type = os.path.splitext(path)[1][1:] #取到文件后綴名 # 判斷文件類型是否滿足條件 if len(file_types)==0: file_types=["py","cpp","c","java","ruby","ini","go","html","css","js","txt","vbs","php","asp","sh"] if file_type in file_types: line_count = count_file_lines(path) return line_count else: file_path = [] for root, dirs, files in os.walk(path): for file in files: file_path.append(os.path.join(root,file)) for f in file_path: file_type = os.path.splitext(f)[1][1:] if len(file_types)==0: file_types= ["py","cpp","c","java","ruby","ini","go","html","css","js","txt","vbs","php","asp","sh"] if file_type not in file_types: continue line_num = count_file_lines(f) line_count += line_num file_lines_dict[f] = line_num return line_count,file_lines_dict if __name__=="__main__": print (sys.argv) if len(sys.argv) < 2: print ("請輸入待統(tǒng)計行數(shù)的代碼絕對路徑!") sys.exit() count_path = sys.argv[1] file_types = [] if len(sys.argv) >2: for i in sys.argv[2:]: file_types.append(i) #print(count_path,file_types) print(count_code_lines(count_path,file_types)) #print(count_file_lines("b.py"))
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python調(diào)用pyttsx3實現(xiàn)離線文字轉(zhuǎn)語音的方式
pyttsx3是 Python 中的文本到語音的離線轉(zhuǎn)換庫,本文給大家介紹Python調(diào)用pyttsx3實現(xiàn)離線文字轉(zhuǎn)語音的方式,感興趣的朋友一起看看吧2024-03-03python讀取.mat文件及將變量存為.mat文件的詳細介紹
這篇文章主要給大家介紹了關(guān)于python讀取.mat文件及將變量存為.mat文件的詳細介紹,?mat文件是matlab的數(shù)據(jù)存儲的標準格式,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-06-06詳解Python?NumPy如何使用argsort方法進行排序
NumPy提供了各種功能強大的數(shù)組操作方法,其中之一就是argsort方法,本文將詳細介紹argsort方法的使用,以及如何在實際項目中充分利用它進行排序操作,希望對大家有所幫助2024-03-03對Python中class和instance以及self的用法詳解
今天小編就為大家分享一篇對Python中class和instance以及self的用法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06