python實現(xiàn)統(tǒng)計代碼行數(shù)的方法
更新時間:2015年05月22日 09:57:47 作者:小小的我
這篇文章主要介紹了python實現(xiàn)統(tǒng)計代碼行數(shù)的方法,涉及Python中os模塊及codecs模塊的相關(guān)使用技巧,需要的朋友可以參考下
本文實例講述了python實現(xiàn)統(tǒng)計代碼行數(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è)計有所幫助。
相關(guān)文章
Python遍歷zip文件輸出名稱時出現(xiàn)亂碼問題的解決方法
這篇文章主要介紹了Python遍歷zip文件輸出名稱時出現(xiàn)亂碼問題的解決方法,實例分析了Python亂碼的出現(xiàn)的原因與相應(yīng)的解決方法,需要的朋友可以參考下2015-04-04PyTorch讀取Cifar數(shù)據(jù)集并顯示圖片的實例講解
今天小編就為大家分享一篇PyTorch讀取Cifar數(shù)據(jù)集并顯示圖片的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07TFRecord格式存儲數(shù)據(jù)與隊列讀取實例
今天小編就為大家分享一篇TFRecord格式存儲數(shù)據(jù)與隊列讀取實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01python調(diào)用騰訊云實名認證接口辨別身份證真假
這篇文章主要為大家介紹了python辨別身份真假之騰訊云身份證實名認證接口,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05詳解Python中如何將數(shù)據(jù)存儲為json格式的文件
這篇文章主要介紹了詳解Python中如何將數(shù)據(jù)存儲為json格式的文件,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Python實現(xiàn)制度轉(zhuǎn)換(貨幣,溫度,長度)
這篇文章主要介紹了Python實現(xiàn)制度轉(zhuǎn)換(貨幣,溫度,長度),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07