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

python学习之——计算给出代码中注释、代码、空行的行数

时间:2015-10-26 18:21:54      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

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

来源:网络

思路:注释行以 ‘#’开头,空行以 ‘\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)
    

 

python学习之——计算给出代码中注释、代码、空行的行数

标签:

原文地址:http://www.cnblogs.com/cloverclt/p/4911839.html

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