python 统计代码行数简单实例


Posted in Python onMay 04, 2017

 python 统计代码行数简单实例

送测的时候,发现需要统计代码行数

于是写了个小程序统计自己的代码的行数。

#calclate_code_lines.py 
import os 
 
def afileline(f_path): 
  res = 0 
  f = open(f_path) 
  for lines in f: 
    if lines.split(): 
      res += 1 
  return res 
 
if __name__=='__main__': 
  host = 'E:'+os.sep+'develop'+os.sep+'dev_workspace'+os.sep+'AptanaStudio3'+os.sep+'webhost' 
   
  allfiles = 0 
  allline = 0 
     
  for root,dirs,files in os.walk(host): 
    for afile in files: 
       
      if(root.startswith(host+os.sep+'entries')): 
        continue 
      elif(root.startswith(host+os.sep+'.svn')): 
        continue 
      elif(root.startswith(host+os.sep+'.settings')): 
        continue 
      elif(root.startswith(host+os.sep+'logs')): 
        continue 
      elif(root.startswith(host+os.sep+'static')): 
        continue  
      elif(root.startswith(host+os.sep+'payload'+os.sep+'.svn')): 
        continue 
      elif(root.startswith(host+os.sep+'dist'+os.sep+'.svn')): 
        continue 
      elif(root.startswith(host+os.sep+'dsync'+os.sep+'.svn')): 
        continue 
      elif(root.startswith(host+os.sep+'hcache'+os.sep+'.svn')): 
        continue 
      elif(root.startswith(host+os.sep+'test'+os.sep+'.svn')): 
        continue 
      elif(root.startswith(host+os.sep+'webhost'+os.sep+'.svn')): 
        continue 
      elif(root.startswith(host+os.sep+'wsgi'+os.sep+'.svn')): 
        continue 
      elif(root.startswith(host+os.sep+'hcache'+os.sep+'templates'+os.sep+'.svn')): 
        continue 
      elif(root.startswith(host+os.sep+'dsync'+os.sep+'hcache'+os.sep+'.svn')): 
        continue 
      else:  
        ext = afile.split('.') 
        ext = ext[-1] 
        if (ext in ['py','css','js','html','txt','docx','wsgi']): 
          itpath = root+os.sep+afile 
          allfiles += 1 
          allline +=afileline(itpath) 
          print (root+os.sep+afile) 
           
  print ('Total: ',allfiles) 
  print ('Total lines:',allline)

之后可以将此改造下便于以后的代码统计

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

Python 相关文章推荐
Python读写txt文本文件的操作方法全解析
Jun 26 Python
python+splinter实现12306网站刷票并自动购票流程
Sep 25 Python
Python3中列表list合并的四种方法
Apr 19 Python
Python更新所有已安装包的操作
Feb 13 Python
python生成大写32位uuid代码
Mar 03 Python
python上selenium的弹框操作实现
Jul 13 Python
Python切片列表字符串如何实现切换
Aug 06 Python
Django日志及中间件模块应用案例
Sep 10 Python
Anaconda+spyder+pycharm的pytorch配置详解(GPU)
Oct 18 Python
运行python提示no module named sklearn的解决方法
Nov 29 Python
基于Python的接口自动化unittest测试框架和ddt数据驱动详解
Jan 27 Python
Pygame游戏开发之太空射击实战敌人精灵篇
Aug 05 Python
利用PyInstaller将python程序.py转为.exe的方法详解
May 03 #Python
Pycharm学习教程(6) Pycharm作为Vim编辑器使用
May 03 #Python
利用numpy+matplotlib绘图的基本操作教程
May 03 #Python
利用matplotlib+numpy绘制多种绘图的方法实例
May 03 #Python
Pycharm学习教程(5) Python快捷键相关设置
May 03 #Python
python中解析json格式文件的方法示例
May 03 #Python
Pycharm学习教程(4) Python解释器的相关配置
May 03 #Python
You might like
PHP图片处理类 phpThumb参数用法介绍
2012/03/11 PHP
php 使用curl模拟登录人人(校内)网的简单实例
2016/06/06 PHP
PHP解耦的三重境界(浅谈服务容器)
2017/03/13 PHP
老生常谈PHP数组函数array_merge(必看篇)
2017/05/25 PHP
JS中 用户登录系统的解决办法
2013/04/15 Javascript
简单谈谈Javascript中类型的判断
2015/10/19 Javascript
XMLHttpRequest对象_Ajax异步请求重点(推荐)
2017/09/28 Javascript
JQuery 又谈ajax局部刷新
2017/11/27 jQuery
详解Angular Forms中自定义ngModel绑定值的方式
2018/12/10 Javascript
[01:08:48]LGD vs OG 2018国际邀请赛淘汰赛BO3 第三场 8.25
2018/08/29 DOTA
[01:03]PWL开团时刻DAY6——别打我
2020/11/05 DOTA
python中requests模块的使用方法
2015/04/08 Python
Python中列表和元组的使用方法和区别详解
2020/12/30 Python
Python实现网络端口转发和重定向的方法
2016/09/19 Python
浅析Python中的赋值和深浅拷贝
2017/08/15 Python
Python logging管理不同级别log打印和存储实例
2018/01/19 Python
python实现npy格式文件转换为txt文件操作
2020/07/01 Python
PyCharm2019 安装和配置教程详解附激活码
2020/07/31 Python
StubHub哥伦比亚:购买和出售您的门票
2016/10/20 全球购物
宝拉珍选官方旗舰店:2%水杨酸精华液,收缩毛孔粗大和祛痘
2018/07/01 全球购物
成都思必达公司C#程序员招聘面试题
2013/06/26 面试题
linux下进程间通信的方式
2014/12/23 面试题
致铅球运动员广播稿精选
2014/01/12 职场文书
对标管理实施方案
2014/03/12 职场文书
医疗纠纷协议书
2014/04/16 职场文书
公司年底活动方案
2014/08/17 职场文书
大学生第一学年自我鉴定2015
2014/09/28 职场文书
房屋租赁合同补充协议
2014/10/11 职场文书
工作证明英文模板
2014/10/21 职场文书
2015年计划生育责任书
2015/05/08 职场文书
反腐倡廉影片观后感
2015/06/08 职场文书
小学班主任心得体会
2016/01/07 职场文书
校园安全学习心得体会
2016/01/18 职场文书
学术会议开幕词
2016/03/03 职场文书
表扬稿表扬信的格式及范文
2019/06/24 职场文书
婚前协议书怎么写,才具有法律效力呢 ?
2019/06/28 职场文书