Python脚本文件LineCount.py的相关代码是什么
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,本篇文章为大家展示了Python脚本文件LineCount.py的相关代码是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。因为最近在作的项目很特殊,所使用
千家信息网最后更新 2026年11月01日Python脚本文件LineCount.py的相关代码是什么
本篇文章为大家展示了Python脚本文件LineCount.py的相关代码是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
因为最近在作的项目很特殊,所使用的语言是一个公司内部的IDE环境,而这个IDE所产生的代码并不是以文本方式存放的,都是放在二进制文件中,而且由于这门语言外界几乎接触不到,所以没有针对它的代码统计程序,当一个模块完成后要统计代码行数会很困难,要统计的话必须先把代码编辑器中的内容拷贝到一个文本类型的文件中。
正好一直在关注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 :这句来判断当前行是不是单行注释(我们的这门语言不支持块注释)以上就是对Python脚本文件LineCount.py的相关代码的介绍。为了能在其他机器上运行,使用了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确实是件很惬意的事。
上述内容就是Python脚本文件LineCount.py的相关代码是什么,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。
代码
文件
脚本
内容
程序
统计
注释
语言
就是
技能
文本
知识
生成
惬意
特殊
简明
臃肿
困难
粗糙
简明扼要
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
手冲咖啡和咖啡机区别
互联网黑科技
苏州找大状互联网科技有限公司
互联网品科技
互联网巨头反垄断金融科技
互联网与科技金融数据
杭州互联网科技公司
奇酷互联网络科技
齐家互联网科技有限公司
互联网科技的未来发展
长亮科技互联网金融
联恩商钥互联网科技有限公司
深圳国际航运科技和互联网
科技互联网的估值
南山科技园互联网公司有哪些
科技互联网大厂
深圳市优智互联网科技有限公司
互联网科技大会大佬
六子台科技互联网展会
互联网科技企业文化
上海喔噻互联网科技有限公司
上海喔噻互联网科技
互联网科技的未来发展
盛创互联网科技有限公司
互联网之后的科技是什么
千方科技属于互联网企业吗
搜狐科技互联网
晋城互联网科技
抖集互联网科技技术咨询
互联网科技的前景