程式碼統計工具(python)

來源:互聯網
上載者:User

用python寫的程式碼統計工具。檔案名稱為counter.py。可以統計代碼檔案中的注釋行、程式碼、空行和總行數。目前支援注釋為//, /*...*/類型和注釋為#類型的程式設計語言程式碼統計。使用方法:

在counter.py的同級目錄建立一個檔案,命名為filelist.txt,在該檔案中輸入需統計的代碼檔案的絕對路徑和檔案名稱。如c:/test.c。每個檔案名稱佔一行。

counter.py代碼如下:

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()
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.