使用python写的代码行数统计程序,python统计程序,import sys;i
文章由Byrx.net分享于2019-03-23 09:03:34
使用python写的代码行数统计程序,python统计程序,import sys;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 != " " : eachLine = 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; lineCount = 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();#该片段来自于http://byrx.net
相关内容
- python测试代理速度源代码,python代理源代码,#!/usr/bin/
- 使用Python脚本格式化压缩后的JS文件,,lines = open
- 字符串去重复值,字符串重复值,以前在实际应用时,需
- 九九乘法表,乘法,print '\\n'.
- urllib2抓取网页时的错误处理,urllib2抓取网页,try: u
- python列出文件夹下的所有文件,python列出文件夹,#方法
- Python队列和堆栈,Python队列堆栈,#For a stack
- python windows平台锁定键盘,python锁定,pywin32中没有Bl
- Python 获取邮件地址,python获取邮件地址,import email
- Python 最长公共子串算法,python串算法,#!/usr/bin/e
评论关闭