首页 > 代码库 > python实现代码行的统计

python实现代码行的统计

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

#-*-coding:utf-8 #!/usr/bin/pythonimport re import osimport 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)                                           

测试文件为:


测试结果:

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