代码行统计工具:根据命令行输入的指定目录及文件类型进行统计目录或单个文件的代码行数

#统计指定目录(count_path)、指定文件类型文件(file_types)的代码量
import os 
def count_all_file_code(count_path,file_types=None): 
 
    #判断传入的文件类型list是否为空,如果为空指定文件类型 
    if len(file_types) == 0: 
        file_types =[".py",".cpp",".java",".c",".h",".php",".asp"] 
 
    #存储所有文件总代码量 
    total_line_number = 0 

    #存储每个文件及代码量     
    file_lines_dict = {}      
    
    #判断目录或文件是否存在 
    if not os.path.exists(count_path):         
        print("输入的目录或文件不存在") 
 
    #判断是否是文件,统计当前文件的代码量
    if os.path.isfile(count_path): 
        #获取文件类型后缀名称,例如 bb.py 中的.py         
        file_type = os.path.splitext(count_path)[1]
        #如果文件类型在指定的文件类型中,调用统计单个文件的函数(调用的函数在后面的代码中),统计单个文件代码          
        if file_type in file_types: 
            total_line_number = count_sigle_file_code(count_path)  
        #单个文件及代码量写入字典        
        file_lines_dict[count_path] = total_line_number
        #返回文件代码量及文件字典                  
        return "文件 %s 代码行数: %d" %(count_path,total_line_number)
     
    #判断是否是目录,使用os.walk()遍历统计目录下所有文件的代码量 
    elif os.path.isdir(count_path): 
        print("进入到判断是否是目录的分支")
        #遍历目录下的所有文件         
        for root,dirs,files in os.walk(count_path): 
            for file in files:  
                file_line_number = 0    #需要先声明一下单个文件代码行数的变量,否则会报错
                #获取文件绝对路径                
                file_path = os.path.join(root,file)
                #print("文件的绝对路径===============:%s" %file_path)       #这行代码是为了调试,查看哪个文件没有统计代码          
                #获取文件类型,如果 bb.py 中的.py                 
                file_type = os.path.splitext(file_path)[1] 
                #如果文件类型在指定的文件类型中,调用统计单个文件的函数,统计单个文件代码 
                if file_type in file_types: 
                    file_line_number = count_sigle_file_code(file_path)
                    #累计代码量                      
                    total_line_number += file_line_number
                #单个文件及代码量写入字典
                print("***文件%s的代码量:%s" %(file_path,file_line_number))                
                file_lines_dict[file_path] = file_line_number
 
    #返回总代码量及文件字典  
    return "总文代码行数: %d\n 每个文件的代码行数: %s" %(total_line_number,file_lines_dict) 
     

 
# 统计单个文件代码量的函数 
def count_sigle_file_code(file_path): 
    
    code_lines = 0   #代码行
    note_lines = 0   #注释行
    space_lines = 0  #空白行
    other_lines = 0  #其他行
    
    #获取编码格式 
    try: 
        fp = open(file_path,encoding="utf-8") 
        encoding_type = "utf-8" 
    except: 
        encoding_type = "gbk" 
    finally: 
        fp.close() 
 
    #打开文件进行统计代码 
    with open(file_path,encoding=encoding_type) as fp: 
        while True:
            #fp.readline():按行读,会读取回车,如果读下一行,继续fp.readline(),没有内容也不会报错
            line = fp.readline()
            
            #如果line为空,表示取到文件末尾,此时break当前while循环
            if not line:   
                break
                
            #如果line以"#"开头,说明是单行注释    
            elif line.strip().startswith("#"):   
                note_lines+=1
                
            #如果line以"'''"或者'"""'开头,说明是多行注释    
            elif line.strip().startswith("'''") or line.strip().startswith('"""'):
                note_lines+=1
                #三单引号和三双引号分开写,是为了防止嵌套情况
                #如果该行的三双引号数量为1,则表示注释分成多行(防止三双引号里面嵌套三单引号的情况)
                if line.count('"""') == 1:    
                    while True:
                        #继续读下一行
                        line = fp.readline()    
                        note_lines+=1
                        #如果该行存在三双引号,则注释结束,break当层while循环
                        if ('"""' in line):   
                            break
                #如果该行的三单引号数量为1,则表示注释分成多行(防止三单引号里面嵌套三双引号的情况)                
                if line.count("'''") == 1:    
                    while True:
                        #继续读下一行
                        line = fp.readline()    
                        note_lines+=1
                        #如果该行存在三单引号,则注释结束,break当层while循环
                        if ("'''" in line):   
                            break
                            
            #行做过strip()之后非空且以字母开头,则是一个代码行                            
            elif line.strip() and "a" <= line.strip()[0].lower() <= "z":    
                #print("代码行:",line.strip())
                code_lines += 1
            #空行                
            elif line.strip() == "":                 
                space_lines += 1
            #其他行
            else:
                other_lines += 1
    #print("code_lines=%s,note_lines=%s,space_lines=%s,other_lines=%s" %(code_lines,note_lines,space_lines,other_lines))
    #返回代码行 
    return code_lines 
 
 
  
if __name__ == "__main__":     
    #控制台执行命令格式如下:
    #E:\2019TestStudy\面试————面试题和简历如何写\代码统计工具>py -3 2.py E:\Python_Question\test_file.py  .py  
    #E:\2019TestStudy\面试————面试题和简历如何写\代码统计工具>py -3 2.py  E:\Python_Question\1first\first_practice .py    
    #执行文件参数  统计目录或文件参数  文件类型参数 
    import sys
       
    #如果长度小于2,证明没有输入目录 
    if len(sys.argv) < 2:         
        print("请输入要统计的文件目录") 
        sys.exit() 
    
    #获取需要统计的目录或文件 
    count_path = sys.argv[1]     
    #文件类型列表 
    file_types = [] 

    #如果命令行参数list长度大于2,证明有目录和文件类型 
    print("打印命令行的内容%s" %sys.argv)
    print("需要统计的目录或文件%s" %count_path)
    if len(sys.argv) > 2:         
        #遍历命令行的文件类型,加入列表 
        for file_type in sys.argv[2:]: 
            file_types.append(file_type) 
    print("文件类型列表%s" %file_types) 
    
    #调用统计代码行函数 
    print(count_all_file_code(count_path,file_types)) 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值