python实现统计代码行数的方法


Posted in Python onMay 22, 2015

本文实例讲述了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程序设计有所帮助。

Python 相关文章推荐
使用Protocol Buffers的C语言拓展提速Python程序的示例
Apr 16 Python
Python使用logging结合decorator模式实现优化日志输出的方法
Apr 16 Python
python条件变量之生产者与消费者操作实例分析
Mar 22 Python
Python实现的插入排序算法原理与用法实例分析
Nov 22 Python
对python中raw_input()和input()的用法详解
Apr 22 Python
记一次python 内存泄漏问题及解决过程
Nov 29 Python
Python 闭包,函数分隔作用域,nonlocal声明非局部变量操作示例
Oct 14 Python
关于多元线性回归分析——Python&amp;SPSS
Feb 24 Python
django修改models重建数据库的操作
Mar 31 Python
如何用Anaconda搭建虚拟环境并创建Django项目
Aug 02 Python
Python 的 __str__ 和 __repr__ 方法对比
Sep 02 Python
pytorch 实现在测试的时候启用dropout
May 27 Python
在Python中处理日期和时间的基本知识点整理汇总
May 22 #Python
python使用PIL模块实现给图片打水印的方法
May 22 #Python
python实现读取命令行参数的方法
May 22 #Python
Python中返回字典键的值的values()方法使用
May 22 #Python
python复制文件的方法实例详解
May 22 #Python
在Python中操作字典之update()方法的使用
May 22 #Python
python判断图片宽度和高度后删除图片的方法
May 22 #Python
You might like
PHP5中的this,self和parent关键字详解教程
2007/03/19 PHP
Web程序工作原理详解
2014/12/25 PHP
CI框架整合smarty步骤详解
2016/05/19 PHP
php版微信公众平台入门教程之开发者认证的方法
2016/09/26 PHP
PHPstorm快捷键(分享)
2017/07/17 PHP
PHP实现对图片的反色处理功能【测试可用】
2018/02/01 PHP
点弹代码 点击页面任何位置都可以弹出页面效果代码
2012/09/17 Javascript
JS调用CS里的带参方法实例
2013/08/01 Javascript
jquery foreach使用示例
2013/09/12 Javascript
document.getElementBy(&quot;id&quot;)与$(&quot;#id&quot;)有什么区别
2013/09/22 Javascript
javascript实现yield的方法
2013/11/06 Javascript
vue router 跳转后回到顶部的实例
2018/08/31 Javascript
微信小程序全局变量GLOBALDATA的定义和调用过程解析
2019/09/23 Javascript
JavaScript点击按钮生成4位随机验证码
2021/01/28 Javascript
typescript编写微信小程序创建项目的方法
2021/01/29 Javascript
浅谈Python类里的__init__方法函数,Python类的构造函数
2016/12/10 Python
python django 访问静态文件出现404或500错误
2017/01/20 Python
详细分析python3的reduce函数
2017/12/05 Python
selenium+python 对输入框的输入处理方法
2018/10/11 Python
python ctypes库2_指定参数类型和返回类型详解
2019/11/19 Python
django 利用Q对象与F对象进行查询的实现
2020/05/15 Python
Django框架请求生命周期实现原理
2020/11/13 Python
基于HTML5的WebSocket的实例代码
2018/08/15 HTML / CSS
用html5绘制折线图的实例代码
2016/03/25 HTML / CSS
Hunkemöller西班牙:欧洲最大的内衣连锁店
2018/08/15 全球购物
牵手50香港:专为黄金岁月的单身人士而设的交友网站
2020/08/14 全球购物
工作自我评价分享
2013/12/01 职场文书
群众路线剖析材料
2014/02/02 职场文书
简单的大学生自我鉴定
2014/02/18 职场文书
yy司仪主持词
2014/03/22 职场文书
奥巴马胜选演讲稿
2014/05/15 职场文书
理财计划书
2014/08/14 职场文书
2014年惩防体系建设工作总结
2014/12/01 职场文书
检讨书范文2000字
2015/01/28 职场文书
教师节座谈会主持词
2015/07/03 职场文书
Python TypeError: ‘float‘ object is not subscriptable错误解决
2022/12/24 Python