Code line statistics Tool (python)

Source: Internet
Author: User

A code line statistics Tool written in python. The file name is counter. py. You can collect comments, code lines, empty lines, and total lines in the code file. Currently, the comments are //,/*... */, and the comments are # type. Usage:

Create a file named filelist.txt in counter.py. in this file, enter the absolute path and file name of the code file to be counted. For example, c:/test. c. Each file name occupies one row.

The counter. py code is as follows:

import recommentline = 0blankline = 0codeline = 0def init():    global commentline, blankline, codeline       commentline = 0    blankline = 0    codeline = 0def output(filename):    global commentline, blankline, codeline        print    print "/*********************************************/"    print "file name: " , filename    print "code line: ", codeline    print "comment line: ", commentline    print "blank line: ", blankline    print "total line: ", codeline + commentline + blankline    print "/*********************************************/"    def count_c_like(filename):    try:        f = open(filename, 'r')    except IOError, e:        print        print e        return None    global commentline, blankline, codeline    iscomment = False    for eachLine in f:        if iscomment:            commentline += 1            if eachLine.find("*/", 0) != -1:                iscomment = False        elif re.match(" *//", eachLine) is not None:            commentline += 1        elif re.match(" */\*", eachLine) is not None:            commentline += 1            if re.search("/\*.*\*/", eachLine) is None:                iscomment = True        elif eachLine.isspace():            blankline += 1        else:            codeline += 1       f.close()    return "OK"def count_mk_like(filename):    try:        f = open(filename, 'r')    except IOError, e:        print        print e        return None    global commentline, blankline, codeline    for eachLine in f:        if eachLine.isspace():            blankline += 1        elif re.match(" *#", eachLine) is not None:            commentline += 1        else:            codeline += 1       f.close()    return "OK"    def main():    try:        f = open("filelist.txt", 'r')    except IOError, e:        print e        raw_input('Press ENTER key to exit')        return None    global filename        for eachLine in f:        filename = eachLine.strip()        if filename == '':            continue                init()        result = "OK"        if re.search("\.min$|\.mk$", filename) is not None:            result = count_mk_like(filename)        else:            result = count_c_like(filename)                    if result == "OK":            output(filename)    f.close()    raw_input('Press ENTER key to exit')if __name__ == '__main__':    main()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.