为了便于统计自己代码的行数,用python实现了该小工具,可以支持对单个文件和一个目录下的多个文件进行统计,并输出统计的代码行数,空行数和注释行数。对不同语言的代码,只需要修改其注释符就可以正确的统计行数信息。
import os, re, sys
class CodeCount:
"""
this py is used to calculate the total number of source,
comment and blank lines.
"""
def __init__(self, path, commentsign='#'):
self.path = path
self.all = 0
self.allComment = 0
self.allBlank = 0
self.allSource = 0
self.commentsign = commentsign
def Calculate(self):
if not os.path.isdir(self.path):
if os.path.exists(self.path):
self.CountLines(self.path)
else:
print '%s is not a valid path or file name!' % path
return
for root, dirs, files in os.walk(self.path):
for file in files:
file = os.path.join(root, file)
self.CountLines(file)
def CountLines(self, filename):
f = file(filename)
lines = f.readlines()
rBlank = re.compile(r'^s*$')
rComment = re.compile('^s*%sw*' % self.commentsign)
nBlank = 0
nComment = 0
nSource = 0
for line in lines:
if rBlank.match(line) or not line:
nBlank += 1
elif rComment.match(line):
nComment += 1
else:
nSource += 1
print filename;
print ' Total :',nBlank+nComment+nSource
print ' Comment :',nComment
print ' Blank :',nBlank
print ' Source :',nSource
self.all += (nBlank+nComment+nSource)
self.allBlank += nBlank
self.allComment += nComment
self.allSource += nSource
def GetCommentCount(self):
return self.allComment
def GetBlankCount(self):
return self.allBlank
def GetSourceCount(self):
return self.allSource
def GetAllCount(self):
return self.allBlank+self.allComment+self.allSource
def main( ):
if len(sys.argv) == 2:
return
#path = raw_input('input a path or filename:')
path = sys.argv[1]
cCut = CodeCount(path)
cCut.Calculate()
print path, ':'
print ' Total :',cCut.GetAllCount()
print ' Comment :',cCut.GetCommentCount()
print ' Blank :',cCut.GetBlankCount()
print ' Source :',cCut.GetSourceCount()
if __name__ == '__main__':
main()
class CodeCount:
"""
this py is used to calculate the total number of source,
comment and blank lines.
"""
def __init__(self, path, commentsign='#'):
self.path = path
self.all = 0
self.allComment = 0
self.allBlank = 0
self.allSource = 0
self.commentsign = commentsign
def Calculate(self):
if not os.path.isdir(self.path):
if os.path.exists(self.path):
self.CountLines(self.path)
else:
print '%s is not a valid path or file name!' % path
return
for root, dirs, files in os.walk(self.path):
for file in files:
file = os.path.join(root, file)
self.CountLines(file)
def CountLines(self, filename):
f = file(filename)
lines = f.readlines()
rBlank = re.compile(r'^s*$')
rComment = re.compile('^s*%sw*' % self.commentsign)
nBlank = 0
nComment = 0
nSource = 0
for line in lines:
if rBlank.match(line) or not line:
nBlank += 1
elif rComment.match(line):
nComment += 1
else:
nSource += 1
print filename;
print ' Total :',nBlank+nComment+nSource
print ' Comment :',nComment
print ' Blank :',nBlank
print ' Source :',nSource
self.all += (nBlank+nComment+nSource)
self.allBlank += nBlank
self.allComment += nComment
self.allSource += nSource
def GetCommentCount(self):
return self.allComment
def GetBlankCount(self):
return self.allBlank
def GetSourceCount(self):
return self.allSource
def GetAllCount(self):
return self.allBlank+self.allComment+self.allSource
def main( ):
if len(sys.argv) == 2:
return
#path = raw_input('input a path or filename:')
path = sys.argv[1]
cCut = CodeCount(path)
cCut.Calculate()
print path, ':'
print ' Total :',cCut.GetAllCount()
print ' Comment :',cCut.GetCommentCount()
print ' Blank :',cCut.GetBlankCount()
print ' Source :',cCut.GetSourceCount()
if __name__ == '__main__':
main()
本文介绍了一个使用Python编写的代码行统计工具,它可以针对单个文件或整个目录进行统计,包括代码行数、空行数及注释行数,并且支持自定义注释符号以适应不同编程语言。
1478

被折叠的 条评论
为什么被折叠?



