zoukankan      html  css  js  c++  java
  • 统计代码中代码行、空行、注释行的个数

    在编写过程中,需要用到glob库,使用方法请参考http://python.jobbole.com/81552/

    下面是统计单个文件的方法

    # coding:utf-8
    
    import os
    import glob
    
    
    def get_file(path):
    
        # filenames = os.listdir(path)
        # myfile = []
        # for file in filenames:
        #     mypath = path + '/' + file
        #     myfile.append(mypath)
        # return myfile
    
        return glob.glob(path + r'/*')
    
    
    def count_lines(files):
        
        codeline, blank, note = 0, 0, 0
    
        for filename in files:
            f = open(filename, 'rb')
            for l in f:
                l = l.strip()
                # sumrize['codeline'] += 1
                codeline += 1
                if l == '':
                    blank += 1
                elif l[0] == '#' or l[0] == '/':
                    note += 1
            f.close()
        return (codeline, blank, note)
    
    if __name__ == '__main__':
        files = get_file('.')
        print files
        lines = count_lines(files)
        print lines[0], lines[1], lines[2]

     下面是统计多个文件的方法

    # coding:utf-8
    
    import os
    import glob
    
    
    def get_file(path):
    
        return glob.glob(path + r'/*')
    
    
    def count_lines(files):
        sumrize = {}
    
        for i in range(len(files)):
            sumrize[i] = {}
            sumrize[i]['codeline'] = 0
            sumrize[i]['blank'] = 0
            sumrize[i]['note'] = 0
    
            f = open(files[i], 'rb')
            for l in f:
                l = l.strip()
                sumrize[i]['codeline'] += 1
                if l == '':
                    sumrize[i]['blank'] += 1
                elif l[0] == '#' or l[0] == '/':
                    sumrize[i]['note'] += 1
            f.close()
            print sumrize[i]
    
    if __name__ == '__main__':
        files = get_file('.')
        count_lines(files)    
  • 相关阅读:
    ThinkCMF框架任意内容包含
    网站防篡改脚本
    Apache Solr Velocity模板远程代码执行
    BurpSuite Intruder模块匹配返回包内容
    Kibana<6.6.0代码执行漏洞复现
    Thinkphp远程代码执行 payload汇总
    常用端口 简洁版
    NISP二级笔记(一) 信息安全管理
    oracle 中的角色
    oracle 用户对象权限
  • 原文地址:https://www.cnblogs.com/milian0711/p/7730911.html
Copyright © 2011-2022 走看看