python 查詢程式碼量
阿新 • • 發佈:2020-12-09
python統計查詢程式碼量
# coding=utf-8 import os import time BASEDIR = r'C:\Users\喬曉強\Downloads\Python-master\Python-master' filelists = [] # 指定想要統計的檔案型別 whitelist = ['py'] # 遍歷檔案, 遞迴遍歷資料夾中的所有 def get_file(basedir): global filelists for parent, dirnames, filenames in os.walk(basedir): # for dirname in dirnames: # getFile(os.path.join(parent,dirname)) #遞迴 for filename in filenames: ext = filename.split('.')[-1] # 只統計指定的檔案型別,略過一些log和cache檔案 if ext in whitelist: filelists.append(os.path.join(parent, filename)) # 統計一個檔案的行數 def count_line(filename): count = 0 for file_line in open(filename, encoding="utf-8").readlines(): if file_line != '' and file_line != '\n': # 過濾掉空行 count += 1 return count if __name__ == '__main__': startTime = time.clock() get_file(BASEDIR) total_line = 0 for file_list in filelists: total_line += count_line(file_list) print('total lines:', total_line)