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

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

創(chuàng)新互聯(lián)專(zhuān)業(yè)為企業(yè)提供安康網(wǎng)站建設(shè)、安康做網(wǎng)站、安康網(wǎng)站設(shè)計(jì)、安康網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、安康企業(yè)網(wǎng)站模板建站服務(wù),10多年安康做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

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'這句是判斷文件的后綴,來(lái)確定是否要計(jì)算代碼行數(shù)。if eachLine.find('--') == 0 :這句來(lái)判斷當(dāng)前行是不是單行注釋?zhuān)ㄎ覀兊倪@門(mén)語(yǔ)言不支持塊注釋?zhuān)?。為了能在其他機(jī)器上運(yùn)行,使用了py2exe來(lái)把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)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。

本文標(biāo)題:Python中怎么實(shí)現(xiàn)統(tǒng)計(jì)行數(shù)
文章源于:http://bm7419.com/article10/igdigo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、品牌網(wǎng)站制作企業(yè)建站、網(wǎng)站營(yíng)銷(xiāo)云服務(wù)器、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營(yíng)