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 18 Python
Python实现简单的文件传输与MySQL备份的脚本分享
Jan 03 Python
Python基于回溯法子集树模板解决取物搭配问题实例
Sep 02 Python
Python实现1-9数组形成的结果为100的所有运算式的示例
Nov 03 Python
python编程线性回归代码示例
Dec 07 Python
python中使用you-get库批量在线下载bilibili视频的教程
Mar 10 Python
Python装饰器实现方法及应用场景详解
Mar 26 Python
通过实例了解python__slots__使用方法
Sep 14 Python
Python3中小括号()、中括号[]、花括号{}的区别详解
Nov 15 Python
python Zmail模块简介与使用示例
Dec 19 Python
python requests模块的使用示例
Apr 07 Python
Python自动化爬取天眼查数据的实现
Jun 15 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 面试碰到过的问题 在此做下记录
2011/06/09 PHP
php获取远程图片的两种 CURL方式和sockets方式获取远程图片
2011/11/07 PHP
实例讲解PHP设计模式编程中的简单工厂模式
2016/02/29 PHP
php array_map使用自定义的函数处理数组中的每个值
2016/10/26 PHP
10个值得深思的PHP面试题
2016/11/14 PHP
PHP钩子实现方法解析
2019/05/21 PHP
BOOM vs RR BO3 第二场2.13
2021/03/10 DOTA
面向对象的编程思想在javascript中的运用上部
2009/11/20 Javascript
使用js检测浏览器的实现代码
2013/05/14 Javascript
动态的创建一个元素createElement及删除一个元素
2014/01/24 Javascript
用html+css+js实现的一个简单的图片切换特效
2014/05/28 Javascript
JavaScript实现动态创建CSS样式规则方案
2014/09/06 Javascript
KnockoutJS 3.X API 第四章之表单submit、enable、disable绑定
2016/10/10 Javascript
jQuery无刷新上传之uploadify简单代码
2017/01/17 Javascript
使用jQuery实现一个类似GridView的编辑,更新,取消和删除的功能
2017/03/15 Javascript
JavaScript代码判断输入的字符串是否含有特殊字符和表情代码实例
2017/08/17 Javascript
js实现鼠标单击Tab表单切换效果
2018/05/16 Javascript
vue router 配置路由的方法
2018/07/26 Javascript
angular 服务随记小结
2019/05/06 Javascript
[01:45]DOTA2新英雄“神谕者”全方位展示
2014/11/21 DOTA
Python运行的17个时新手常见错误小结
2012/08/07 Python
在Python的while循环中使用else以及循环嵌套的用法
2015/10/14 Python
win7上python2.7连接mysql数据库的方法
2017/01/14 Python
对python抓取需要登录网站数据的方法详解
2018/05/21 Python
django+xadmin+djcelery实现后台管理定时任务
2018/08/14 Python
pygame实现简易飞机大战
2018/09/11 Python
如何使用Python实现斐波那契数列
2019/07/02 Python
Django文件存储 默认存储系统解析
2019/08/02 Python
Python实现实时数据采集新型冠状病毒数据实例
2020/02/04 Python
Stefania Mode英国:奢华设计师和时尚服装
2017/10/23 全球购物
台湾全方位线上课程与职能学习平台:TibaMe
2019/12/04 全球购物
介绍Java的内部类
2012/10/27 面试题
房地产经营管理专业自荐信
2014/09/02 职场文书
2015年宣传部工作总结范文
2015/03/31 职场文书
2016年“5.12”国际护士节活动总结
2016/04/06 职场文书
vue报错function () { [native code] },无法出现我们想要的内容 Unknown custom element
2022/04/11 Vue.js