激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - python實現統計代碼行數的方法

python實現統計代碼行數的方法

2020-07-05 10:15小小的我 Python

這篇文章主要介紹了python實現統計代碼行數的方法,涉及Python中os模塊及codecs模塊的相關使用技巧,需要的朋友可以參考下

本文實例講述了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
60
61
62
63
64
65
'''
Author: liupengfei
Function: count lines of code in a folder iteratively
Shell-format: cmd [dir]
Attention: default file encode is utf8 and default file type is java-source-file. But users can customize this script by just modifing global variables.
'''
import sys
import os
import codecs
from _pyio import open
totalCount = 0;
fileType = '.java'
descLineBegin = '//'
descBlockBegin = r'/**'
descBlockEnd = r'*/'
fileEncode = 'utf-8'
def main():
  DIR = os.getcwd()
  if len(sys.argv) >= 2:
    DIR = sys.argv[1]
  if os.path.exists(DIR) and os.path.isdir(DIR):
    print('target directory is %s' % DIR)
    countDir(DIR)
    print('total code line is %d' % totalCount)
  else:
    print('target should be a directory!')
def isFileType(file):
  return len(fileType) + file.find(fileType) == len(file)
def countDir(DIR):
  for file in os.listdir(DIR):
    absPath = DIR + os.path.sep + file;
    if os.path.exists(absPath):
      if os.path.isdir(absPath):
        countDir(absPath)
      elif isFileType(absPath):
        try:
          countFile(absPath)
        except UnicodeDecodeError:
          print(
            '''encode of %s is different, which
is not supported in this version!'''
            )
def countFile(file):
  global totalCount
  localCount = 0
  isInBlockNow = False
  f = codecs.open(file, 'r', fileEncode);
  for line in f:
    if (not isInBlockNow) and line.find(descLineBegin) == 0:
      pass;
    elif (not isInBlockNow) and line.find(descBlockBegin) >= 0:
      if line.find(descBlockBegin) > 0:
        localCount += 1
      isInBlockNow = True;
    elif isInBlockNow and line.find(descBlockEnd) >= 0:
      if line.find(descBlockEnd) + len(descBlockEnd) < len(line):
        localCount += 1
      isInBlockNow = False;
    elif (not isInBlockNow) and len(line.replace('\\s+', '')) > 0:
      localCount += 1
  f.close()
  totalCount += localCount
  print('%s : %d' % (file, localCount))
if __name__ == '__main__':
  main();

希望本文所述對大家的Python程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: xxxxxx中国 | 精品国产91一区二区三区 | 免费午夜视频在线观看 | 欧洲成人综合网 | 在线播放的av网站 | 97zyz成人免费视频 | www亚洲免费| 亚洲成人福利在线观看 | 精品国产一区二区在线 | 成人毛片在线观看 | 国产免费www | 国产成人高清成人av片在线看 | 91精品国产综合久久久欧美 | 日韩高清影视 | 免费一区在线 | 欧美日韩网站在线观看 | 亚洲一级簧片 | 国产精品亚洲激情 | 99在线热播精品免费 | 成人午夜免费国产 | 午夜在线视频观看 | 黄色高清视频网站 | 国产99久久久久久免费看 | 国产精品视频一区二区三区四区国 | a黄色网| 欧美精品a∨在线观看不卡 午夜精品影院 | 法国性xxx精品hd专区 | 久久精品亚洲欧美日韩精品中文字幕 | 国产成人高潮免费观看精品 | 日韩视频一区二区三区在线观看 | 欧美成人毛片 | 成人一级黄色片 | 日韩av电影在线免费观看 | 黄色特级片黄色特级片 | 国产精品成人一区二区三区吃奶 | 久久久久久亚洲国产精品 | 久久久久亚洲a | 久久探花 | 99亚洲伊人久久精品影院红桃 | 极品五月天 | 亚洲一区二区三区在线看 |