最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2015年05月22日 09:57:47   作者:小小的我  
這篇文章主要介紹了python實現統計代碼行數的方法,涉及Python中os模塊及codecs模塊的相關使用技巧,需要的朋友可以參考下

本文實例講述了python實現統計代碼行數的方法。分享給大家供大家參考。具體實現方法如下:

'''
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程序設計有所幫助。

相關文章

最新評論

安龙县| 老河口市| 扶风县| 军事| 宣城市| 禄丰县| 万宁市| 谷城县| 宣汉县| 柞水县| 西吉县| 马边| 乐清市| 秦安县| 河池市| 宜春市| 北京市| 仁怀市| 洞口县| 永清县| 吴堡县| 柳州市| 滦平县| 安陆市| 宁蒗| 东宁县| 岐山县| 五寨县| 巫溪县| 鞍山市| 寿光市| 临泽县| 遵化市| 平顶山市| 二连浩特市| 辽宁省| 鄱阳县| 革吉县| 灵丘县| 宜宾市| 阿拉尔市|