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 开发Activex组件方法
Nov 08 Python
Python正则表达式知识汇总
Sep 22 Python
基于Python实现的ID3决策树功能示例
Jan 02 Python
python之文件读取一行一行的方法
Jul 12 Python
pycharm修改界面主题颜色的方法
Jan 17 Python
python循环定时中断执行某一段程序的实例
Jun 29 Python
基于python实现学生信息管理系统
Nov 22 Python
Django框架中间件定义与使用方法案例分析
Nov 28 Python
Python爬虫爬取煎蛋网图片代码实例
Dec 16 Python
Python编程快速上手——PDF文件操作案例分析
Feb 28 Python
Python填充任意颜色,不同算法时间差异分析说明
May 16 Python
Python调用C语言程序方法解析
Jul 07 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
JavaScript脚本性能的优化方法
2007/02/02 Javascript
Firefox outerHTML实现代码
2009/06/04 Javascript
利用Jquery实现可多选的下拉框
2014/02/21 Javascript
js闭包的用途详解
2014/11/09 Javascript
Nodejs为什么选择javascript为载体语言
2015/01/13 NodeJs
JavaScript获取URL中参数querystring的方法详解
2016/10/11 Javascript
JavaScript数组去重的几种方法效率测试
2016/10/23 Javascript
详解vue2路由vue-router配置(懒加载)
2017/04/08 Javascript
vue实现个人信息查看和密码修改功能
2018/05/06 Javascript
vue 修改 data 数据问题并实时显示的方法
2018/08/27 Javascript
微信小程序dom操作的替代思路实例分析
2018/12/06 Javascript
微信小程序非swiper组件实现的自定义伪3D轮播图效果示例
2018/12/11 Javascript
JavaScript递归函数定义与用法实例分析
2019/01/24 Javascript
[01:38]DOTA2辉夜杯 欢乐的观众现场采访
2015/12/26 DOTA
[05:26]TI10典藏宝瓶套装外观展示
2020/07/03 DOTA
Python Web框架Flask中使用百度云存储BCS实例
2015/02/08 Python
python动态参数用法实例分析
2015/05/25 Python
Python数组定义方法
2016/04/13 Python
Python循环语句中else的用法总结
2016/09/11 Python
浅谈Tensorflow由于版本问题出现的几种错误及解决方法
2018/06/13 Python
Windows下将Python文件打包成.EXE可执行文件的方法
2018/08/03 Python
python后端接收前端回传的文件方法
2019/01/02 Python
Python StringIO及BytesIO包使用方法解析
2020/06/15 Python
浅谈css3中calc在less编译时被计算的解决办法
2017/12/04 HTML / CSS
俄罗斯最大的消费电子连锁零售商:Mvideo
2017/06/25 全球购物
阿迪达斯法国官方网站:adidas法国
2018/03/20 全球购物
RetroStage德国:复古服装
2019/02/03 全球购物
什么是接口(Interface)?
2013/02/01 面试题
什么是测试驱动开发(TDD)
2012/02/15 面试题
土建资料员岗位职责
2014/01/04 职场文书
采购类个人求职的自我评价
2014/02/18 职场文书
初中班主任经验交流材料
2014/05/16 职场文书
三好生演讲稿
2014/09/12 职场文书
2014年作风建设剖析材料
2014/10/23 职场文书
道歉短信大全
2015/05/12 职场文书
解决golang结构体tag编译错误的问题
2021/05/02 Golang