编写Python程序实现行数统计
编写Python程序实现行数统计
当我们在使用Python编程语言进行程序开发的时候,会发现这一功能强大的语言可以给我们带来非常大的作用。那么,接下来我们将会通过一个Python程序的实现,来仔细分析一下这语言给我们带来的独特之处。
- 深入探讨Python ConfigParser模块正确应
- Python解密VBS基本应用代码讲解
- Python CGI环境在不同操作系统中配置方法
- Python算法正确实现方式介绍
- Python print正确使用方法浅析
正好一直在关注Python,还没有用Python写过程序,今天就利用中午休息的时间写了一个简单的Python程序用于代码统计。对输入的路径作递归,查找代码文件,对每一个代码文件计算它的注释行数,空行数,真正的代码行数。自己用的程序,就写的粗糙了,也没加异常处理。
主要的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"],
- )
不过生成exe后程序臃肿很多,有3M多。感觉使用Python程序确实是件很惬意的事。
评论关闭