scios
 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行\n\'%(i,file_list[i],lines )
63 
64     title=\'统计结果\'
65     msg=\'目前代码行数:%d\n完成进度:%.2f%%\n距离十万行代码还差%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)

 

分类:

技术点:

相关文章:

  • 2022-02-09
  • 2021-11-29
  • 2022-01-05
  • 2022-02-03
  • 2022-03-08
  • 2022-12-23
  • 2022-01-16
  • 2022-12-23
猜你喜欢
  • 2021-12-15
  • 2022-01-22
  • 2022-02-13
  • 2022-01-13
  • 2021-08-05
  • 2021-08-05
相关资源
相似解决方案