Python实现代码统计工具


Posted in Python onSeptember 19, 2019

本文实例为大家分享了Python实现代码统计工具的具体代码,供大家参考,具体内容如下

思路:首先获取所有文件,然后统计每个文件中代码的行数,最后将行数相加.

实现的功能:

统计每个文件的行数;

统计总行数;

支持指定统计文件类型,排除不想统计的文件类型;

排除空行;

排除注释行

import os
import sys
import os.path
#for i in sys.argv:
# print (i)

# 判断单个文件的代码行数
def count_file_lines(file_path):
 line_count = 0
 flag=True
 try:
 fp = open(file_path,"r",encoding="utf-8")
 encoding_type="utf-8"
 fp.close()
 except:
 encoding_type="gbk"
 with open(file_path,"r",encoding=encoding_type) as fp:
 for line in fp:
 #print (line_count)
  if line.strip()=="":
  continue
  else:
  if line.strip().endswith("'''") and flag == False:
   flag=True
   continue
  if line.strip().endswith('"""') and flag == False:
   flag=True
   continue
  if flag == False:
   continue
  if line.strip().startswith("#encoding") or line.strip().startswith("#-*-"):
   line_count += 1
  #elif line.strip().startswith('"""') and line.strip().endswith('"""') and line.strip()!='"""':
 #continue
  #elif line.strip().startswith("'''") and line.strip().endswith("'''") and line.strip()!="'''":
 #continue
  elif line.strip().startswith('#'):
   continue
  elif line.strip().startswith("'''") and flag == True:
   flag = False
   continue
  elif line.strip().startswith('"""') and flag == True:
   flag = False
   continue
  else:
   line_count += 1
 return line_count

def count_code_lines(path,file_types=[]):
 # 判断路径是否存在
 if not os.path.exists(path):
 print("您输入的目录或文件路径不存在")
 return 0

 line_count=0 #代码行总数
 file_lines_dict={} #每个文件代码行数

 # 判断是否为文件
 if os.path.isfile(path):
 file_type = os.path.splitext(path)[1][1:] #取到文件后缀名

 # 判断文件类型是否满足条件
 if len(file_types)==0:
 file_types=["py","cpp","c","java","ruby","ini","go","html","css","js","txt","vbs","php","asp","sh"]
 if file_type in file_types:
 line_count = count_file_lines(path)
 return line_count
 else:
 file_path = []
 for root, dirs, files in os.walk(path):
  for file in files:
  file_path.append(os.path.join(root,file))
  for f in file_path:
   file_type = os.path.splitext(f)[1][1:]
   if len(file_types)==0:
   file_types=

["py","cpp","c","java","ruby","ini","go","html","css","js","txt","vbs","php","asp","sh"]
   if file_type not in file_types:
   continue
   line_num = count_file_lines(f)
   line_count += line_num
   file_lines_dict[f] = line_num
  return line_count,file_lines_dict
 

if __name__=="__main__":
 print (sys.argv)
 if len(sys.argv) < 2:
 print ("请输入待统计行数的代码绝对路径!")
 sys.exit()
 count_path = sys.argv[1]
 file_types = []
 if len(sys.argv) >2:
 for i in sys.argv[2:]:
  file_types.append(i)

#print(count_path,file_types)
print(count_code_lines(count_path,file_types))
#print(count_file_lines("b.py"))

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中unittest用法实例
Sep 25 Python
学习python中matplotlib绘图设置坐标轴刻度、文本
Feb 07 Python
对numpy Array [: ,] 的取值方法详解
Jul 02 Python
一百行python代码将图片转成字符画
Feb 19 Python
python实现QQ邮箱/163邮箱的邮件发送
Jan 22 Python
python多线程抽象编程模型详解
Mar 20 Python
Python 根据日志级别打印不同颜色的日志的方法示例
Aug 08 Python
Python常用模块logging——日志输出功能(示例代码)
Nov 20 Python
解决python3插入mysql时内容带有引号的问题
Mar 02 Python
Numpy数组的广播机制的实现
Nov 03 Python
Python实现Appium端口检测与释放的实现
Dec 31 Python
Python绘制K线图之可视化神器pyecharts的使用
Mar 02 Python
python实现统计代码行数的小工具
Sep 19 #Python
python日志模块logbook使用方法
Sep 19 #Python
python统计指定目录内文件的代码行数
Sep 19 #Python
python如何从文件读取数据及解析
Sep 19 #Python
python实现代码统计器
Sep 19 #Python
python实现代码统计程序
Sep 19 #Python
python tkinter图形界面代码统计工具(更新)
Sep 18 #Python
You might like
截获网站title标签之家内容的例子
2006/10/09 PHP
php小技巧 把数组的键和值交换形成了新的数组,查找值取得键
2011/06/02 PHP
PHP全概率运算函数(优化版) Webgame开发必备
2011/07/04 PHP
PHP判断是否为空的几个函数对比
2015/04/21 PHP
Laravel实现自定义错误输出内容的方法
2016/10/10 PHP
该如何加载google-analytics(或其他第三方)的JS
2010/05/13 Javascript
JS幻灯片可循环播放可平滑旋转带滚动导航(自写)
2013/08/05 Javascript
javascript 弹出的窗口返回值给父窗口具体实现
2013/11/23 Javascript
在javascript中实现函数数组的方法
2013/12/25 Javascript
node.js中的fs.closeSync方法使用说明
2014/12/17 Javascript
AngularJS学习笔记之TodoMVC的分析
2015/02/22 Javascript
javascript实现信息增删改查的方法
2015/07/25 Javascript
js实现简单的省市县三级联动效果实例
2016/02/18 Javascript
js实现的光标位置工具函数示例
2016/10/03 Javascript
浅谈Angular HttpClient简单入门
2018/05/04 Javascript
微信小程序页面传多个参数跳转页面的实现方法
2019/05/17 Javascript
详解微信小程序支付流程与梳理
2019/07/16 Javascript
layui表格设计以及数据初始化详解
2019/10/26 Javascript
在Vue 中获取下拉框的文本及选项值操作
2020/08/13 Javascript
Vue+scss白天和夜间模式切换功能的实现方法
2021/01/05 Vue.js
Python 2.7.x 和 3.x 版本的重要区别小结
2014/11/28 Python
Django中URL视图函数的一些高级概念介绍
2015/07/20 Python
Python实现OpenCV的安装与使用示例
2018/03/30 Python
Django Xadmin多对多字段过滤实例
2020/04/07 Python
python实现画图工具
2020/08/27 Python
西雅图的买手店:Totokaelo
2019/10/19 全球购物
Kiwi.com中国:找到特价机票并发现新目的地
2019/10/27 全球购物
北京RT科技有限公司.net工程师面试题
2013/02/15 面试题
采购员岗位职责
2013/11/15 职场文书
药剂专业个人求职信范文
2014/04/29 职场文书
2014年人事科工作总结
2014/11/19 职场文书
生日赠语
2015/06/23 职场文书
培训感想范文
2015/08/07 职场文书
心理健康教育培训研修感言
2015/11/18 职场文书
基于Redis延迟队列的实现代码
2021/05/13 Redis
MySQL中几种插入和批量语句实例详解
2021/09/14 MySQL