python實現代碼統(tǒng)計器
更新時間:2019年09月19日 09:02:25 作者:zhengjuNEW
這篇文章主要為大家詳細介紹了python實現代碼統(tǒng)計器,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python中的代碼行數統(tǒng)計,供大家參考,具體內容如下
思路:統(tǒng)計文件中代碼的總行數減去空行單行注釋以及多行注釋
功能:
1.獲取文件內容的總行數
2.排除空行 單行注釋 多行注釋
def code_statistics(path): # # 打開這個文件 with open(path, 'r', encoding='utf-8') as openFile: # 按列讀取 fileline = openFile.readlines() # 給非代碼行一個變量 i = 0 # 整個文件里面內容的總行數 number_line = len(fileline) # 給多行注釋一個狀態(tài) note = False # 遍歷文件內容 for line in fileline: # 空行 if line == '\n': i += 1 # 單行注釋 elif re.findall('[#]', line): i += 1 # 多行注釋開頭 elif re.findall("\'\'\'", line) and note == False: i += 1 note = True # 多行注釋結尾 elif re.findall("\'\'\'", line) and note == True: i += 1 note = False # 多行注釋內部注釋 elif note: i += 1 num_code_line = number_line - i print(num_code_line)
如果統(tǒng)計文件夾中的python文件的代碼行數,首先就是要遍歷文件目錄,篩選出以.py結尾的文件,再去統(tǒng)計py文件里面的代碼行數
def get_all_fire(path): # 得到當前目錄下的所有文件 file_list = os.listdir(path) py_file_abs = [] # 遍歷所有文件 for file_name in file_list: # 獲取文件及文件夾的絕對路徑 file_abs = os.path.join(path, file_name) if os.path.isfile(file_abs) and file_name.endswith('.py'): # 判斷當前文件路徑是否是文件和.py文件 py_file_abs.append(file_abs) # 判斷當前文件路徑是不是文件夾 elif os.path.isdir(file_abs): py_file_abs += get_all_fire(file_abs) return py_file_abs
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
pytorch中retain_graph==True的作用說明
這篇文章主要介紹了pytorch中retain_graph==True的作用說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02