溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python中怎么實(shí)現(xiàn)統(tǒng)計行數(shù)

發(fā)布時間:2021-07-10 15:37:34 來源:億速云 閱讀:170 作者:Leah 欄目:編程語言

Python中怎么實(shí)現(xiàn)統(tǒng)計行數(shù),很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

Python程序腳本文件LineCount.py的內(nèi)容如下:

import sys;  import os;  class LineCount:  def trim(self,docstring):  if not docstring:  return ''  lines = docstring.expandtabs().splitlines()  indent = sys.maxint  for line in lines[1:]:  stripped = line.lstrip()  if stripped:  indent = min(indent, len(line) - len(stripped))  trimmed = [lines[0].strip()]  if indent < sys.maxint: for line in lines[1:]:  trimmed.append(line[indent:].rstrip())  while trimmed and not trimmed[-1]:  trimmed.pop()  while trimmed and not trimmed[0]:  trimmed.pop(0)  return '\n'.join(trimmed)  def FileLineCount(self,filename):  (filepath,tempfilename) = os.path.split(filename);  (shotname,extension) = os.path.splitext(tempfilename);  if extension == '.txt' or extension == '.hol' : # file type   file = open(filename,'r');  self.sourceFileCount += 1;  allLines = file.readlines();  file.close();  lineCount =0;  commentCount = 0;  blankCount = 0;  codeCount = 0;  for eachLine in allLines:  if eachLine != " " :  eachLineeachLine = eachLine.replace(" ",""); #remove space  eachLine = self.trim(eachLine); #remove tabIndent  if eachLine.find('--') == 0 : #LINECOMMENT   commentCount += 1;  else :  if eachLine == "":  blankCount += 1;  else :  codeCount += 1;  lineCountlineCount = lineCount + 1;  self.all += lineCount;  self.allComment += commentCount;  self.allBlank += blankCount;  self.allSource += codeCount;  print filename;  print ' Total :',lineCount ;  print ' Comment :',commentCount;  print ' Blank :',blankCount;  print ' Source :',codeCount;  def CalulateCodeCount(self,filename):  if os.path.isdir(filename) :  if not filename.endswith('\\'):  filename += '\\';   for file in os.listdir(filename):  if os.path.isdir(filename + file):  self.CalulateCodeCount(filename + file);  else:  self.FileLineCount(filename + file);  else:  self.FileLineCount(filename);  # Open File  def __init__(self):  self.all = 0;  self.allComment =0;  self.allBlank = 0;  self.allSource = 0;  self.sourceFileCount = 0;  filename = raw_input('Enter file name: ');  self.CalulateCodeCount(filename);  if self.sourceFileCount == 0 :  print 'No Code File';  pass;  print '\n';  print '***************** All Files **********************';  print ' Files :',self.sourceFileCount;  print ' Total :',self.all;  print ' Comment :',self.allComment;  print ' Blank :',self.allBlank;  print ' Source :',self.allSource;  print '****************************************************';  myLineCount = LineCount();

可以看到extension == '.txt' or extension == '.hol'這句是判斷文件的后綴,來確定是否要計算代碼行數(shù)。if eachLine.find('--') == 0 :這句來判斷當(dāng)前行是不是單行注釋(我們的這門語言不支持塊注釋)。為了能在其他機(jī)器上運(yùn)行,使用了py2exe來把Python腳本生成可執(zhí)行的exe,setup.py腳本內(nèi)容如下:

from distutils.core import setup  import py2exe  setup(  version = "0.0.1",  description = "LineCount",  name = "LineCount",  console = ["LineCount.py"],  )

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI