代码行数统计工具

本文介绍了一个使用Python编写的代码行统计工具,它可以针对单个文件或整个目录进行统计,包括代码行数、空行数及注释行数,并且支持自定义注释符号以适应不同编程语言。
Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

为了便于统计自己代码的行数,用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()
    

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值