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程序中自定义异常的方法
Oct 16 Python
python将unicode转为str的方法
Jun 21 Python
使用matplotlib画散点图的方法
May 25 Python
[原创]Python入门教程3. 列表基本操作【定义、运算、常用函数】
Oct 30 Python
在IPython中进行Python程序执行时间的测量方法
Nov 01 Python
Python文本文件的合并操作方法代码实例
Mar 31 Python
python 连续不等式语法糖实例
Apr 15 Python
Python通过Pillow实现图片对比
Apr 29 Python
Python中使用filter过滤列表的一个小技巧分享
May 02 Python
哪种Python框架适合你?简单介绍几种主流Python框架
Aug 04 Python
Python matplotlib多个子图绘制整合
Apr 13 Python
LeetCode189轮转数组python示例
Aug 05 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
php数组函数序列之array_intersect() 返回两个或多个数组的交集数组
2011/11/10 PHP
php删除页面记录 同时刷新页面 删除条件用GET方式获得
2012/01/10 PHP
PHP高级对象构建 工厂模式的使用
2012/02/05 PHP
深入Apache与Nginx的优缺点比较详解
2013/06/17 PHP
CodeIgniter输出中文乱码的两种解决办法
2014/06/12 PHP
phpmyadmin下载、安装、配置教程
2017/05/16 PHP
2017年最好用的9个php开发工具推荐(超好用)
2017/10/23 PHP
document.getElementById方法在Firefox与IE中的区别
2010/05/18 Javascript
javascript调试说明
2010/06/07 Javascript
JS 添加网页桌面快捷方式的代码详细整理
2012/12/27 Javascript
省市联动效果的简单实现代码(推荐)
2016/06/06 Javascript
几句话带你理解JS中的this、闭包、原型链
2016/09/26 Javascript
NodeJS自定义模块写法(详解)
2017/06/27 NodeJs
webpack-dev-server自动更新页面方法
2018/02/22 Javascript
基于vue通用表单解决方案的思考与分析
2019/03/16 Javascript
nodejs微信开发之接入指南
2019/03/17 NodeJs
详细分析vue响应式原理
2020/06/22 Javascript
js实现随机点名器精简版
2020/06/29 Javascript
vue实现简单全选和反选功能
2020/09/15 Javascript
微信小程序实现购物车功能
2020/11/18 Javascript
[40:03]DOTA2上海特级锦标赛主赛事日 - 1 败者组第一轮#1EHOME VS Archon
2016/03/02 DOTA
[00:58]他们到底在电话里听到了什么?
2017/11/21 DOTA
python获取Linux下文件版本信息、公司名和产品名的方法
2014/10/05 Python
python在linux系统下获取系统内存使用情况的方法
2015/05/11 Python
python实现对excel进行数据剔除操作实例
2017/12/07 Python
python把1变成01的步骤总结
2019/02/27 Python
以设计师精品品质提供快速时尚:PopJulia
2018/01/09 全球购物
美国最佳选择产品网站:Best Choice Products
2019/05/27 全球购物
德国亚洲食品网上商店:asiafoodland.de
2019/12/28 全球购物
请用Java实现列出某个目录下的所有文件
2013/09/23 面试题
函授本科自我鉴定
2013/11/03 职场文书
人力资源经理自我评价
2014/01/04 职场文书
买卖协议书范本
2014/04/21 职场文书
业务员岗位职责
2015/02/03 职场文书
2015年小学重阳节活动总结
2015/07/29 职场文书
JavaScript实现九宫格拖拽效果
2022/06/28 Javascript