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获取Linux系统下的本机IP地址代码分享
Nov 07 Python
深入Python函数编程的一些特性
Apr 13 Python
使用python获取电脑的磁盘信息方法
Nov 01 Python
在Python中Dataframe通过print输出多行时显示省略号的实例
Dec 22 Python
Django框架搭建的简易图书信息网站案例
May 25 Python
对python3 sort sorted 函数的应用详解
Jun 27 Python
python循环定时中断执行某一段程序的实例
Jun 29 Python
Python如何在DataFrame增加数值
Feb 14 Python
在Django中预防CSRF攻击的操作
Mar 13 Python
Nginx+Uwsgi+Django 项目部署到服务器的思路详解
May 08 Python
用pandas划分数据集实现训练集和测试集
Jul 20 Python
基于Python采集爬取微信公众号历史数据
Nov 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
Discuz! 5.0.0论坛程序中加入一段js代码,让会员点击下载附件前自动弹出提示窗口
2007/04/18 PHP
简单PHP上传图片、删除图片实现代码
2010/05/12 PHP
PHP异步调用socket实现代码
2012/01/12 PHP
使用Apache的htaccess防止图片被盗链的解决方法
2013/04/27 PHP
PHP、Nginx、Apache中禁止网页被iframe引用的方法
2020/10/01 PHP
解密ThinkPHP3.1.2版本之模块和操作映射
2014/06/19 PHP
PHP使用CURL实现对带有验证码的网站进行模拟登录的方法
2014/07/23 PHP
php中通过DirectoryIterator删除整个目录的方法
2015/03/13 PHP
详解Yii2 定制表单输入字段的标签和样式
2017/01/04 PHP
JQuery Study Notes 学习笔记(一)
2010/08/04 Javascript
优化Jquery,提升网页加载速度
2013/11/14 Javascript
js获取ajax返回值代码
2014/04/30 Javascript
牛叉的Jquery——Jquery与DOM对象的互相转换及DOM的三种操作
2015/10/29 Javascript
js+css绘制颜色动态变化的圈中圈效果
2016/01/27 Javascript
Bootstrap面板(Panels)的简单实现代码
2017/03/17 Javascript
浅谈vue.js中v-for循环渲染
2017/07/26 Javascript
基于JSON数据格式详解
2017/08/31 Javascript
js实现关闭网页出现是否离开提示
2017/12/07 Javascript
微信小程序页面滚动到指定位置代码实例
2019/09/07 Javascript
py中的目录与文件判别代码
2008/07/16 Python
Python转码问题的解决方法
2008/10/07 Python
python使用BeautifulSoup分析网页信息的方法
2015/04/04 Python
Fabric 应用案例
2016/08/28 Python
Python  pip安装lxml出错的问题解决办法
2017/02/10 Python
Python基于列表模拟堆栈和队列功能示例
2018/01/05 Python
python利用跳板机ssh远程连接redis的方法
2019/02/19 Python
Python+Pyqt实现简单GUI电子时钟
2021/02/22 Python
Python使用matplotlib绘制Logistic曲线操作示例
2019/11/28 Python
Django多个app urls配置代码实例
2020/11/26 Python
施华洛世奇天猫官方旗舰店:SWAROVSKI
2017/04/17 全球购物
番木瓜健康和保健产品第一大制造商:Herbal Papaya
2017/04/25 全球购物
英国礼品和生活方式品牌:Treat Republic
2020/11/21 全球购物
挂职自我鉴定
2014/02/26 职场文书
寄语学生的话
2014/04/10 职场文书
《打电话》教学反思
2016/02/22 职场文书
win11自动弹出虚拟键盘怎么关闭? Win11关闭虚拟键盘的技巧
2023/01/09 数码科技