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 相关文章推荐
linux系统使用python监测系统负载脚本分享
Jan 15 Python
PyQt5每天必学之事件与信号
Apr 20 Python
python中itertools模块zip_longest函数详解
Jun 12 Python
pytorch训练imagenet分类的方法
Jul 27 Python
浅析Python3中的对象垃圾收集机制
Jun 06 Python
pyqt 实现QlineEdit 输入密码显示成圆点的方法
Jun 24 Python
Django项目使用CircleCI的方法示例
Jul 14 Python
python读写csv文件并增加行列的实例代码
Aug 01 Python
VSCode基础使用与VSCode调试python程序入门的图文教程
Mar 30 Python
python + selenium 刷B站播放量的实例代码
Jun 12 Python
Python流程控制语句的深入讲解
Jun 15 Python
Python基础教程(一)——Windows搭建开发Python开发环境
Jul 20 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
一个用于MySQL的PHP XML类
2006/10/09 PHP
非常重要的php正则表达式详解
2016/01/04 PHP
PHP中的使用curl发送请求(GET请求和POST请求)
2017/02/08 PHP
javascript编程起步(第一课)
2007/01/10 Javascript
Json2Template.js 基于jquery的插件 绑定JavaScript对象到Html模板中
2011/10/29 Javascript
html中的input标签的checked属性jquery判断代码
2012/09/19 Javascript
Js nodeType 属性全面解析
2013/11/14 Javascript
Javascript对象属性方法汇总
2013/11/21 Javascript
24款热门实用的jQuery插件推荐
2014/12/24 Javascript
JS简单实现浮动窗口效果示例
2016/09/07 Javascript
node.js中http模块和url模块的简单介绍
2017/10/06 Javascript
再谈Angular4 脏值检测(性能优化)
2018/04/23 Javascript
Nodejs 发布自己的npm包并制作成命令行工具的实例讲解
2018/05/15 NodeJs
Vue 处理表单input单行文本框的实例代码
2019/05/09 Javascript
python socket 超时设置 errno 10054
2014/07/01 Python
Python多线程编程(二):启动线程的两种方法
2015/04/05 Python
详解Python3 中hasattr()、getattr()、setattr()、delattr()函数及示例代码数
2018/04/18 Python
Php多进程实现代码
2018/05/07 Python
基于python3实现socket文件传输和校验
2018/07/28 Python
python面试题小结附答案实例代码
2019/04/11 Python
pyqt5 键盘监听按下enter 就登陆的实例
2019/06/25 Python
Python OpenCV调用摄像头检测人脸并截图
2020/08/20 Python
Python解决pip install时出现的Could not fetch URL问题
2019/08/01 Python
OpenCV python sklearn随机超参数搜索的实现
2020/01/17 Python
NumPy统计函数的实现方法
2020/01/21 Python
python3检查字典传入函数键是否齐全的实例
2020/06/05 Python
HTML5实现移动端弹幕动画效果
2019/08/01 HTML / CSS
美国女性服饰销售网站:Nasty Gal(坏女孩)
2016/07/26 全球购物
建筑项目策划书
2014/01/13 职场文书
重点工程汇报材料
2014/08/27 职场文书
七一讲话心得体会
2014/09/05 职场文书
事业单位人员的自我评价范文
2014/09/21 职场文书
2014党委书记四风问题对照检查材料思想汇报
2014/09/22 职场文书
环境工程专业毕业生求职信
2014/09/30 职场文书
大学生党性分析材料
2014/12/19 职场文书
html中显示特殊符号(附带特殊字符对应表)
2021/06/21 HTML / CSS