码迷,mamicode.com
首页 > 编程语言 > 详细

python实现代码行的统计

时间:2014-05-21 15:06:30      阅读:379      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   c   code   ext   

最近闲来无事写了一个统计C或者C++代码行数的程序,主要用到了python以及正则表达式

#-*-coding:utf-8 
#!/usr/bin/python
import re 
import os
import sys
‘‘‘get the file or dir in one path‘‘‘
def getfilename(path):
    if os.path.exists(path):
        filelist = os.listdir(path)
    return filelist

‘‘‘get the file with the ending of .cpp‘‘‘
def isfile(filelist):
    ‘‘‘match the that end with .cpp‘‘‘
    pattern = re.compile(r‘^\w.{0,}cpp$‘)  
    filetemp = []
    for file in filelist:
        if os.path.isfile("F:\\"+file)and pattern.match(file):
            filetemp.append("F:\\"+file)
    return filetemp

‘‘‘caculate the codeline and the commentline‘‘‘
def caculate(filelist):
    ‘‘‘match the beginning of /* and the ending of */‘‘‘
    fullpattern = re.compile(r"^(/\*)(.+)(\*/$)")  
    ‘‘‘only match the beginning of /*‘‘‘
    beginpattern = re.compile(r"^(/\*)")   
    ‘‘‘only match the ending of */‘‘‘       
    endpattern = re.compile(r"\*/$")    
    ‘‘‘match the blank line‘‘‘           
    pattern = re.compile("\\s+$")                 
    comentline = 0                        
    codeline = 0          
    filenum = 0                 
    for file in filelist:
        try:
            f = open(file,‘r‘)
        except:
            print "open file error"
        filenum = filenum + 1
        print "begin to caculate the" + str(filenum) + "‘s" + " file"     
        flag = 0
        while 1:
            line = f.readline()
            if not line:
                break
            if pattern.match(line):
                continue
            ‘‘‘remove the blank line‘‘‘
            line2 = line.strip()            
            if fullpattern.match(line2):
                comentline = comentline + 1
            elif beginpattern.match(line2):
                comentline = comentline + 1
                flag = 1
            elif endpattern.match(line2):
                comentline = comentline + 1
                flag = 0
            elif flag == 1:
                comentline = comentline +1
            else:
                codeline = codeline + 1
        try:
            f.close()
        except:
            print "close file error"
    
    return (comentline, codeline)
            
if __name__ == "__main__":
        
        dirlist =  getfilename("F:\\")              
        filelist = isfile(dirlist)
        if filelist:
            print " find C or C++ file"
        else:
            print "not find C or C++ file"
            sys.exit(0)
        (comentline,codeline) = caculate(filelist)
        print "comenline is: " + str(comentline)
        print "codeline is: " + str(codeline)
        
        

        
  

        
        
 

测试文件为:


bubuko.com,布布扣

测试结果:

bubuko.com,布布扣

从上面可以看到我我们统计了两个文件,之所以少一个是因为其中一个文件的后缀为.c而程序只会匹配 以.cpp结尾的文件,统计的文件一个为空。所以我们的结果只是其中一个文件(结果是正确没有注释,不算空行有34条代码)当然想统计空行也很容易(正则表达式上面已经给出只需做稍微修改即可bubuko.com,布布扣

 

python实现代码行的统计,布布扣,bubuko.com

python实现代码行的统计

标签:style   blog   class   c   code   ext   

原文地址:http://blog.csdn.net/zh533749/article/details/26342155

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!