题目:计算给出代码中注释、代码、空行的行数

来源:网络

思路:注释行以 ‘#’开头,空行以 ‘\n’ 开头,以此作为判断

def count_linenum(fname):
    fobj = open(fname,"rU")
    #print fobj.readlines()
    count_blankline = 0
    count_notes = 0
    count_code = 0
    for eachLine in fobj:
        if eachLine[0] == '\n':
            count_blankline += 1
        elif eachLine[0] == '#':
            count_notes += 1
        else:
            count_code += 1
    print "count_blankline:%d" %count_blankline
    print "count_notes:%d" %count_notes
    print "count_code:%d" %count_notes

    fobj.close()

if __name__ == '__main__':
    filename = raw_input("please enter filename:")
    count_linenum(filename)
    

 

posted on 2015-10-26 17:28  laclt  阅读(254)  评论(0编辑  收藏  举报