网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Python中怎么实现统计行数

Python中怎么实现统计行数,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

创新互联专业为企业提供安康网站建设、安康做网站、安康网站设计、安康网站制作等企业网站建设、网页设计与制作、安康企业网站模板建站服务,10多年安康做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

Python程序脚本文件LineCount.py的内容如下:

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'这句是判断文件的后缀,来确定是否要计算代码行数。if eachLine.find('--') == 0 :这句来判断当前行是不是单行注释(我们的这门语言不支持块注释)。为了能在其他机器上运行,使用了py2exe来把Python脚本生成可执行的exe,setup.py脚本内容如下:

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

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。


当前题目:Python中怎么实现统计行数
网址分享:http://bjjierui.cn/article/igdigo.html

其他资讯