python 统计代码行数简单实例

时间:2022-10-07 13:24:13

 python 统计代码行数简单实例

送测的时候,发现需要统计代码行数

于是写了个小程序统计自己的代码的行数。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#calclate_code_lines.py
import os
  
def afileline(f_path):
  res = 0
  f = open(f_path)
  for lines in f:
    if lines.split():
      res += 1
  return res
  
if __name__=='__main__':
  host = 'E:'+os.sep+'develop'+os.sep+'dev_workspace'+os.sep+'AptanaStudio3'+os.sep+'webhost'
    
  allfiles = 0
  allline = 0
      
  for root,dirs,files in os.walk(host):
    for afile in files:
        
      if(root.startswith(host+os.sep+'entries')):
        continue
      elif(root.startswith(host+os.sep+'.svn')):
        continue
      elif(root.startswith(host+os.sep+'.settings')):
        continue
      elif(root.startswith(host+os.sep+'logs')):
        continue
      elif(root.startswith(host+os.sep+'static')):
        continue 
      elif(root.startswith(host+os.sep+'payload'+os.sep+'.svn')):
        continue
      elif(root.startswith(host+os.sep+'dist'+os.sep+'.svn')):
        continue
      elif(root.startswith(host+os.sep+'dsync'+os.sep+'.svn')):
        continue
      elif(root.startswith(host+os.sep+'hcache'+os.sep+'.svn')):
        continue
      elif(root.startswith(host+os.sep+'test'+os.sep+'.svn')):
        continue
      elif(root.startswith(host+os.sep+'webhost'+os.sep+'.svn')):
        continue
      elif(root.startswith(host+os.sep+'wsgi'+os.sep+'.svn')):
        continue
      elif(root.startswith(host+os.sep+'hcache'+os.sep+'templates'+os.sep+'.svn')):
        continue
      elif(root.startswith(host+os.sep+'dsync'+os.sep+'hcache'+os.sep+'.svn')):
        continue
      else
        ext = afile.split('.')
        ext = ext[-1]
        if (ext in ['py','css','js','html','txt','docx','wsgi']):
          itpath = root+os.sep+afile
          allfiles += 1
          allline +=afileline(itpath)
          print (root+os.sep+afile)
            
  print ('Total: ',allfiles)
  print ('Total lines:',allline)

之后可以将此改造下便于以后的代码统计

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://blog.csdn.net/jiyingying_up/article/details/10962749