python實(shí)現(xiàn)統(tǒng)計(jì)代碼行數(shù)的方法
本文實(shí)例講述了python實(shí)現(xiàn)統(tǒng)計(jì)代碼行數(shù)的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
''' Author: liupengfei Function: count lines of code in a folder iteratively Shell-format: cmd [dir] Attention: default file encode is utf8 and default file type is java-source-file. But users can customize this script by just modifing global variables. ''' import sys import os import codecs from _pyio import open totalCount = 0; fileType = '.java' descLineBegin = '//' descBlockBegin = r'/**' descBlockEnd = r'*/' fileEncode = 'utf-8' def main(): DIR = os.getcwd() if len(sys.argv) >= 2: DIR = sys.argv[1] if os.path.exists(DIR) and os.path.isdir(DIR): print('target directory is %s' % DIR) countDir(DIR) print('total code line is %d' % totalCount) else: print('target should be a directory!') def isFileType(file): return len(fileType) + file.find(fileType) == len(file) def countDir(DIR): for file in os.listdir(DIR): absPath = DIR + os.path.sep + file; if os.path.exists(absPath): if os.path.isdir(absPath): countDir(absPath) elif isFileType(absPath): try: countFile(absPath) except UnicodeDecodeError: print( '''encode of %s is different, which is not supported in this version!''' ) def countFile(file): global totalCount localCount = 0 isInBlockNow = False f = codecs.open(file, 'r', fileEncode); for line in f: if (not isInBlockNow) and line.find(descLineBegin) == 0: pass; elif (not isInBlockNow) and line.find(descBlockBegin) >= 0: if line.find(descBlockBegin) > 0: localCount += 1 isInBlockNow = True; elif isInBlockNow and line.find(descBlockEnd) >= 0: if line.find(descBlockEnd) + len(descBlockEnd) < len(line): localCount += 1 isInBlockNow = False; elif (not isInBlockNow) and len(line.replace('\\s+', '')) > 0: localCount += 1 f.close() totalCount += localCount print('%s : %d' % (file, localCount)) if __name__ == '__main__': main();
希望本文所述對大家的Python程序設(shè)計(jì)有所幫助。
- python統(tǒng)計(jì)指定目錄內(nèi)文件的代碼行數(shù)
- python 統(tǒng)計(jì)代碼行數(shù)簡單實(shí)例
- Python腳本實(shí)現(xiàn)代碼行數(shù)統(tǒng)計(jì)代碼分享
- python實(shí)現(xiàn)代碼行數(shù)統(tǒng)計(jì)示例分享
- python實(shí)現(xiàn)代碼統(tǒng)計(jì)器
- python實(shí)現(xiàn)代碼統(tǒng)計(jì)程序
- python tkinter圖形界面代碼統(tǒng)計(jì)工具(更新)
- python tkinter圖形界面代碼統(tǒng)計(jì)工具
- python實(shí)現(xiàn)統(tǒng)計(jì)代碼行數(shù)的小工具
相關(guān)文章
Python遍歷zip文件輸出名稱時(shí)出現(xiàn)亂碼問題的解決方法
這篇文章主要介紹了Python遍歷zip文件輸出名稱時(shí)出現(xiàn)亂碼問題的解決方法,實(shí)例分析了Python亂碼的出現(xiàn)的原因與相應(yīng)的解決方法,需要的朋友可以參考下2015-04-04PyTorch讀取Cifar數(shù)據(jù)集并顯示圖片的實(shí)例講解
今天小編就為大家分享一篇PyTorch讀取Cifar數(shù)據(jù)集并顯示圖片的實(shí)例講解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07TFRecord格式存儲(chǔ)數(shù)據(jù)與隊(duì)列讀取實(shí)例
今天小編就為大家分享一篇TFRecord格式存儲(chǔ)數(shù)據(jù)與隊(duì)列讀取實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01詳細(xì)解析Python中的變量的數(shù)據(jù)類型
這篇文章主要介紹了詳細(xì)解析Python中的變量的數(shù)據(jù)類型,是Python學(xué)習(xí)當(dāng)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-05-05python調(diào)用騰訊云實(shí)名認(rèn)證接口辨別身份證真假
這篇文章主要為大家介紹了python辨別身份真假之騰訊云身份證實(shí)名認(rèn)證接口,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05詳解Python中如何將數(shù)據(jù)存儲(chǔ)為json格式的文件
這篇文章主要介紹了詳解Python中如何將數(shù)據(jù)存儲(chǔ)為json格式的文件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Python實(shí)現(xiàn)制度轉(zhuǎn)換(貨幣,溫度,長度)
這篇文章主要介紹了Python實(shí)現(xiàn)制度轉(zhuǎn)換(貨幣,溫度,長度),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07