這篇文章主要介紹python怎么實(shí)現(xiàn)代碼統(tǒng)計(jì)器,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
思路:統(tǒng)計(jì)文件中代碼的總行數(shù)減去空行單行注釋以及多行注釋
功能:
1.獲取文件內(nèi)容的總行數(shù)
2.排除空行 單行注釋 多行注釋
def code_statistics(path): # # 打開(kāi)這個(gè)文件 with open(path, 'r', encoding='utf-8') as openFile: # 按列讀取 fileline = openFile.readlines() # 給非代碼行一個(gè)變量 i = 0 # 整個(gè)文件里面內(nèi)容的總行數(shù) number_line = len(fileline) # 給多行注釋一個(gè)狀態(tài) note = False # 遍歷文件內(nèi)容 for line in fileline: # 空行 if line == '\n': i += 1 # 單行注釋 elif re.findall('[#]', line): i += 1 # 多行注釋開(kāi)頭 elif re.findall("\'\'\'", line) and note == False: i += 1 note = True # 多行注釋結(jié)尾 elif re.findall("\'\'\'", line) and note == True: i += 1 note = False # 多行注釋內(nèi)部注釋 elif note: i += 1 num_code_line = number_line - i print(num_code_line)
如果統(tǒng)計(jì)文件夾中的python文件的代碼行數(shù),首先就是要遍歷文件目錄,篩選出以.py結(jié)尾的文件,再去統(tǒng)計(jì)py文件里面的代碼行數(shù)
def get_all_fire(path): # 得到當(dāng)前目錄下的所有文件 file_list = os.listdir(path) py_file_abs = [] # 遍歷所有文件 for file_name in file_list: # 獲取文件及文件夾的絕對(duì)路徑 file_abs = os.path.join(path, file_name) if os.path.isfile(file_abs) and file_name.endswith('.py'): # 判斷當(dāng)前文件路徑是否是文件和.py文件 py_file_abs.append(file_abs) # 判斷當(dāng)前文件路徑是不是文件夾 elif os.path.isdir(file_abs): py_file_abs += get_all_fire(file_abs) return py_file_abs
以上是“python怎么實(shí)現(xiàn)代碼統(tǒng)計(jì)器”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)站題目:python怎么實(shí)現(xiàn)代碼統(tǒng)計(jì)器-創(chuàng)新互聯(lián)
轉(zhuǎn)載來(lái)于:http://jinyejixie.com/article6/dpphog.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開(kāi)發(fā)、全網(wǎng)營(yíng)銷推廣、品牌網(wǎng)站制作、網(wǎng)站收錄、網(wǎng)站排名、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容
移動(dòng)網(wǎng)站建設(shè)知識(shí)