欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實(shí)現(xiàn)統(tǒng)計(jì)代碼行數(shù)的方法

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

本文實(shí)例講述了python實(shí)現(xiàn)統(tǒng)計(jì)代碼行數(shù)的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

'''
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程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • python煙花效果的代碼實(shí)例

    python煙花效果的代碼實(shí)例

    在本篇文章里小編給大家整理的是關(guān)于python煙花效果的代碼實(shí)例,對此有興趣的朋友們可以在跟著學(xué)習(xí)參考下。
    2020-02-02
  • Python遍歷zip文件輸出名稱時(shí)出現(xiàn)亂碼問題的解決方法

    Python遍歷zip文件輸出名稱時(shí)出現(xiàn)亂碼問題的解決方法

    這篇文章主要介紹了Python遍歷zip文件輸出名稱時(shí)出現(xiàn)亂碼問題的解決方法,實(shí)例分析了Python亂碼的出現(xiàn)的原因與相應(yīng)的解決方法,需要的朋友可以參考下
    2015-04-04
  • PyTorch讀取Cifar數(shù)據(jù)集并顯示圖片的實(shí)例講解

    PyTorch讀取Cifar數(shù)據(jù)集并顯示圖片的實(shí)例講解

    今天小編就為大家分享一篇PyTorch讀取Cifar數(shù)據(jù)集并顯示圖片的實(shí)例講解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • TFRecord格式存儲(chǔ)數(shù)據(jù)與隊(duì)列讀取實(shí)例

    TFRecord格式存儲(chǔ)數(shù)據(jù)與隊(duì)列讀取實(shí)例

    今天小編就為大家分享一篇TFRecord格式存儲(chǔ)數(shù)據(jù)與隊(duì)列讀取實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • Python獲取svn版本信息

    Python獲取svn版本信息

    本文主要介紹了Python獲取svn版本信息,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • 詳細(xì)解析Python中的變量的數(shù)據(jù)類型

    詳細(xì)解析Python中的變量的數(shù)據(jù)類型

    這篇文章主要介紹了詳細(xì)解析Python中的變量的數(shù)據(jù)類型,是Python學(xué)習(xí)當(dāng)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-05-05
  • python調(diào)用騰訊云實(shí)名認(rèn)證接口辨別身份證真假

    python調(diào)用騰訊云實(shí)名認(rèn)證接口辨別身份證真假

    這篇文章主要為大家介紹了python辨別身份真假之騰訊云身份證實(shí)名認(rèn)證接口,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • 詳解Python中如何將數(shù)據(jù)存儲(chǔ)為json格式的文件

    詳解Python中如何將數(shù)據(jù)存儲(chǔ)為json格式的文件

    這篇文章主要介紹了詳解Python中如何將數(shù)據(jù)存儲(chǔ)為json格式的文件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Python實(shí)現(xiàn)制度轉(zhuǎn)換(貨幣,溫度,長度)

    Python實(shí)現(xiàn)制度轉(zhuǎn)換(貨幣,溫度,長度)

    這篇文章主要介紹了Python實(shí)現(xiàn)制度轉(zhuǎn)換(貨幣,溫度,長度),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Python繪制K線圖之可視化神器pyecharts的使用

    Python繪制K線圖之可視化神器pyecharts的使用

    這篇文章主要介紹了Python繪制K線圖之可視化神器pyecharts的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03

最新評(píng)論