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實(shí)現(xiàn)將多個(gè)文件的名稱或后綴名由大寫改為小寫
這篇文章主要介紹了如何基于Python語言實(shí)現(xiàn)將多個(gè)文件的名稱或后綴名由大寫字母修改為小寫,文中的示例代碼講解詳細(xì),感興趣的可以了解下2023-09-09
pandas 數(shù)據(jù)類型轉(zhuǎn)換的實(shí)現(xiàn)
這篇文章主要介紹了pandas 數(shù)據(jù)類型轉(zhuǎn)換的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
python實(shí)現(xiàn)五子棋人機(jī)對戰(zhàn)游戲
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)五子棋之人機(jī)對戰(zhàn)游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06
python內(nèi)置函數(shù)compile(),complex()的使用
這篇文章主要為大家詳細(xì)介紹了python內(nèi)置函數(shù)compile(),complex()的使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
Python使用scrapy采集時(shí)偽裝成HTTP/1.1的方法
這篇文章主要介紹了Python使用scrapy采集時(shí)偽裝成HTTP/1.1的方法,實(shí)例分析了scrapy采集的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
numpy如何取出對角線元素、計(jì)算對角線元素和np.diagonal
這篇文章主要介紹了numpy如何取出對角線元素、計(jì)算對角線元素和np.diagonal問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
Python實(shí)現(xiàn)快速大文件比較代碼解析
這篇文章主要介紹了Python實(shí)現(xiàn)快速大文件比較代碼解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
python 對給定可迭代集合統(tǒng)計(jì)出現(xiàn)頻率,并排序的方法
今天小編就為大家分享一篇python 對給定可迭代集合統(tǒng)計(jì)出現(xiàn)頻率,并排序的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10

