zoukankan      html  css  js  c++  java
  • Python 统计代码量

     1 #统计代码量,显示离10W行代码还有多远
     2 #递归搜索各个文件夹
     3 #显示各个类型的源文件和源代码数量
     4 #显示总行数与百分比
     5 
     6 import os
     7 import easygui as g
     8 
     9 #查找文件
    10 def find_file(file_path,target):
    11     os.chdir(file_path)
    12     all_files=os.listdir(os.curdir)
    13     for each in all_files:
    14         #print(each)
    15         fext=os.path.splitext(each)[1]
    16         if fext in target:
    17             lines=calc_code(each) #统计行数
    18             #print("文件%s的代码行数是%d"%(each,lines))
    19             #统计文件数
    20             try:
    21                 file_list[fext]+=1
    22             except KeyError:
    23                 file_list[fext]=1
    24             #统计源代码行数
    25             try:
    26                 source_list[fext] += lines
    27                 #print(source_list[fext])
    28             except KeyError:
    29                 source_list[fext] = lines
    30                 #print(source_list[fext])
    31 
    32 
    33 
    34         if os.path.isdir(each):
    35             find_file(each,target) # 递归调用
    36             os.chdir(os.pardir) #返回上层目录
    37 
    38 
    39 #统计行数
    40 def calc_code(file_name):
    41     lines=0
    42     with open(file_name,encoding='gb18030',errors='ignore') as f:
    43         print("正在分析文件%s..."%file_name)
    44         try:
    45             for eachline in f:
    46                 lines += 1
    47         except UnicodeDecodeError:
    48             pass
    49         print("文件%s分析完毕,包含代码行%d." %(file_name,lines))
    50     return lines
    51 
    52 
    53 #显示结果
    54 def show_result(start_dir):
    55     lines=0
    56     total=0
    57     text=''
    58 
    59     for i in source_list:
    60         lines=source_list[i]
    61         total+=lines
    62         text+='%s源文件%d个,源代码%d行
    '%(i,file_list[i],lines )
    63 
    64     title='统计结果'
    65     msg='目前代码行数:%d
    完成进度:%.2f%%
    距离十万行代码还差%d行'%(total,total/1000,100000-total)
    66     g.msgbox(msg,title,text)
    67 
    68 #file_path=input("要查找的路径; ")  # C:\Users\54353\PycharmProjects\untitled
    69 target=['.py','.java','.c','.cc','.cpp']  #定义需要查找的源文件类型
    70 file_list={}
    71 source_list={}
    72 g.msgbox('请打开您的文件夹','统计代码量')
    73 path=g.diropenbox('请选择您的代码库:')
    74 
    75 find_file(path,target)
    76 show_result(path)
  • 相关阅读:
    lambda 和 iterable
    使用Jenkins部署Python项目
    python下selenium自动化测试自我实践
    【其它】数学学科编码
    【其它】音阶中的数学
    【数理统计基础】 06
    【数理统计基础】 05
    【数理统计基础】 04
    【数理统计基础】 03
    【数理统计基础】 02
  • 原文地址:https://www.cnblogs.com/scios/p/8386618.html
Copyright © 2011-2022 走看看