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 相关文章推荐
python装饰器使用方法实例
Nov 21 Python
在Python中使用next()方法操作文件的教程
May 24 Python
详解Python如何生成词云的方法
Jun 01 Python
python和pygame实现简单俄罗斯方块游戏
Feb 19 Python
浅析Python 3 字符串中的 STR 和 Bytes 有什么区别
Oct 14 Python
在Python中append以及extend返回None的例子
Jul 20 Python
python正则过滤字母、中文、数字及特殊字符方法详解
Feb 11 Python
Python图像处理库PIL的ImageGrab模块介绍详解
Feb 26 Python
Python Scrapy框架:通用爬虫之CrawlSpider用法简单示例
Apr 11 Python
python文件操作seek()偏移量,读取指正到指定位置操作
Jul 05 Python
详解pytorch tensor和ndarray转换相关总结
Sep 03 Python
如何利用python读取micaps文件详解
Oct 18 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
PHP安装问题
2006/10/09 PHP
怎样在UNIX系统下安装php3
2006/10/09 PHP
Php header()函数语法及使用代码
2013/11/04 PHP
PHP彩蛋信息介绍和阻止泄漏的方法(隐藏功能)
2014/08/06 PHP
PHP 验证登陆类分享
2015/03/13 PHP
php简单获取目录列表的方法
2015/03/24 PHP
PHP面向对象多态性实现方法简单示例
2017/09/27 PHP
js类中获取外部函数名的方法
2007/08/19 Javascript
JavaScript对象模型-执行模型
2008/04/28 Javascript
jQuery 常见开发使用技巧总结
2009/12/26 Javascript
js+css使DIV始终居于屏幕中间 左下 左上 右上 右下的代码集合
2011/03/10 Javascript
JS实现字体选色板实例代码
2013/11/20 Javascript
几种设置表单元素中文本输入框不可编辑的方法总结
2013/11/25 Javascript
node.js应用后台守护进程管理器Forever安装和使用实例
2014/06/01 Javascript
实例详解angularjs和ajax的结合使用
2015/10/22 Javascript
bootstrap下拉列表与输入框组结合的样式调整
2016/10/08 Javascript
Bootstrap select实现下拉框多选效果
2016/12/23 Javascript
react 创建单例组件的方法
2018/04/26 Javascript
js jquery 获取某一元素到浏览器顶端的距离实现方法
2018/09/05 jQuery
jQuery插件实现非常实用的tab栏切换功能【案例】
2019/02/18 jQuery
详解vue路由
2020/08/05 Javascript
Python中的__SLOTS__属性使用示例
2015/02/18 Python
python3使用QQ邮箱发送邮件
2020/05/20 Python
Django 创建/删除用户的示例代码
2019/07/24 Python
django drf框架中的user验证以及JWT拓展的介绍
2019/08/12 Python
python实现二分类的卡方分箱示例
2019/11/22 Python
Dr.Jart+美国官网:韩国药妆品牌
2019/01/18 全球购物
英国邮购活的植物主要供应商:Gardening Direct
2019/01/28 全球购物
出纳担保书范文
2014/04/02 职场文书
《雕塑之美》教学反思
2014/04/24 职场文书
《李广射虎》教学反思
2014/04/27 职场文书
投资建议书模板
2014/05/12 职场文书
酒店开业庆典策划方案
2014/05/28 职场文书
Python3的进程和线程你了解吗
2022/03/16 Python
使用MybatisPlus打印sql语句
2022/04/22 SQL Server
在 Python 中利用 Pool 进行多线程
2022/04/24 Python