python实现统计代码行数的方法
作者:小小的我 发布时间:2021-06-23 11:24:17
标签:python,统计
本文实例讲述了python实现统计代码行数的方法。分享给大家供大家参考。具体实现方法如下:
'''
Author: liupengfei
Function: count lines of code in a folder iteratively
Shell-format: cmd [dir]
Attention: default file encode is utf8 and default file type is java-source-file. But users can customize this script by just modifing global variables.
'''
import sys
import os
import codecs
from _pyio import open
totalCount = 0;
fileType = '.java'
descLineBegin = '//'
descBlockBegin = r'/**'
descBlockEnd = r'*/'
fileEncode = 'utf-8'
def main():
DIR = os.getcwd()
if len(sys.argv) >= 2:
DIR = sys.argv[1]
if os.path.exists(DIR) and os.path.isdir(DIR):
print('target directory is %s' % DIR)
countDir(DIR)
print('total code line is %d' % totalCount)
else:
print('target should be a directory!')
def isFileType(file):
return len(fileType) + file.find(fileType) == len(file)
def countDir(DIR):
for file in os.listdir(DIR):
absPath = DIR + os.path.sep + file;
if os.path.exists(absPath):
if os.path.isdir(absPath):
countDir(absPath)
elif isFileType(absPath):
try:
countFile(absPath)
except UnicodeDecodeError:
print(
'''encode of %s is different, which
is not supported in this version!'''
)
def countFile(file):
global totalCount
localCount = 0
isInBlockNow = False
f = codecs.open(file, 'r', fileEncode);
for line in f:
if (not isInBlockNow) and line.find(descLineBegin) == 0:
pass;
elif (not isInBlockNow) and line.find(descBlockBegin) >= 0:
if line.find(descBlockBegin) > 0:
localCount += 1
isInBlockNow = True;
elif isInBlockNow and line.find(descBlockEnd) >= 0:
if line.find(descBlockEnd) + len(descBlockEnd) < len(line):
localCount += 1
isInBlockNow = False;
elif (not isInBlockNow) and len(line.replace('\\s+', '')) > 0:
localCount += 1
f.close()
totalCount += localCount
print('%s : %d' % (file, localCount))
if __name__ == '__main__':
main();
希望本文所述对大家的Python程序设计有所帮助。


猜你喜欢
- 例如1441,那么会产生“运行时错误”,报错信息类似下面:SessionID 错误 'ASP 0164 : 80004005'
- 1。如果客户端和服务器端的连接需要跨越并通过不可信任的网络,那么就需要使用SSH隧道来加密该连接的通信。 2。用set password语句
- 当我们建好数据库及表后,首先想到的就是向数据库的表中输入数据.下面我们就来探讨一下如何向数据库增加数据:1.常用的方法是insert语句in
- 本文实例讲述了Django框架模型简单介绍与使用。分享给大家供大家参考,具体如下:ORM介绍ORM Object relational ma
- 后台数据库: [Microsoft Access] 与 [Microsoft Sql Server] 更换之后,ASP代码应注意要修改的一些
- jqueryjQueryJQUERYJqueryJQueryjquery报错jsJSJsmyeclipseMyEclipseMyeclips
- 本文实例讲述了Python单例模式。分享给大家供大家参考,具体如下:单例模式:保证一个类仅有一个实例,并提供一个访问他的全局访问点。实现某个
- 阅读目录什么是设计模式单体模式:工厂模式:单例模式观察者模式(发布订阅模式)策略模式模板模式代理模式外观模式设计模式太多了,貌似有23种,其
- php mysql PDO 查询操作的实例详解<?php $dbh = new PDO('mysql:host=localho
- Win10系统安装MySQL8.0遇到的问题及解决方法,具体内容如下所示:对着第一个桌面应用击右键,选择“以管理员身份运行”选项,就可以以管
- 同事在学mybatis时,遇到了一个问题就是,使用char类型字段作为查询条件时一直都查不出数据,其他类型的则可以。 使用的数据库
- 1.游标方式 代码如下:DECLARE @Data NVARCHAR(max) SET @Data='1,tanw,2,
- 在ASP编程中,身份认证可以说是常要用到的。但怎么样才能做到认证的安全呢?表单提交页面:sub.htm  
- 一、数据库介绍1、为什么要学习数据库通常,我们存储数据,直接用本地文件即可,但是,本地文件不利于存放海量数据,也不利于用程序对文件的数据进行
- 在看fastai的代码时,看到这么一段:n=100x = torch.ones(n,2) x[:,0].uniform_(-1.,1)x[:
- 一、如何设置utf8mb4mysql中针对字符串类型,在设置charset的时候可以精确到字段。如果只将某个字段设置utf8mb4,那么其他
- 大家一定使用过 phpmyadmin 里面的数据库导入,导出功能,非常方便。但是在实际应用中,我发现如下几个问题: 1、数据库超过一定尺寸,
- @Test public void test33() {
- 本文实例讲述了Python设计模式之观察者模式。分享给大家供大家参考,具体如下:观察者模式是一个软件设计模式,一个主题对象包涵一系列依赖他的
- LSTM介绍关于LSTM的具体原理,可以参考:https://www.jb51.net/article/178582.htmhttps://