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将图片文件转换成base64编码的方法
Mar 14 Python
python创建一个最简单http webserver服务器的方法
May 08 Python
Python实现计算最小编辑距离
Mar 17 Python
全面了解python字符串和字典
Jul 07 Python
Django应用程序中如何发送电子邮件详解
Feb 04 Python
分析Python中解析构建数据知识
Jan 20 Python
pyinstaller打包程序exe踩过的坑
Nov 19 Python
Python使用socket_TCP实现小文件下载功能
Oct 09 Python
python 元组和列表的区别
Dec 30 Python
Python+MySQL随机试卷及答案生成程序的示例代码
Feb 01 Python
如何用Python编写一个电子考勤系统
Feb 08 Python
用gpu训练好的神经网络,用tensorflow-cpu跑出错的原因及解决方案
Mar 03 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
laravel在中间件内生成参数并且传递到控制器中的2种姿势
2019/10/15 PHP
thinkphp5 + ajax 使用formdata提交数据(包括文件上传) 后台返回json完整实例
2020/03/02 PHP
浅谈php常用的7大框架的优缺点
2020/07/20 PHP
JavaScript 格式字符串的应用
2010/03/29 Javascript
基于JQuery实现CheckBox全选全不选
2011/06/27 Javascript
jQuery 瀑布流 绝对定位布局(二)(延迟AJAX加载图片)
2012/05/23 Javascript
jQuery 借助插件Lavalamp实现导航条动态美化效果
2013/09/27 Javascript
JS简单的图片放大缩小的两种方法
2013/11/11 Javascript
禁止ajax缓存获取程序最新数据的方法
2013/11/19 Javascript
jquery右下角自动弹出可关闭的广告层
2015/05/08 Javascript
简介JavaScript中substring()方法的使用
2015/06/06 Javascript
深入理解JavaScript 函数
2016/06/06 Javascript
基于Vue.js实现简单搜索框
2020/03/26 Javascript
老生常谈js数据类型
2017/08/03 Javascript
详解Angular cli配置过程记录
2019/11/07 Javascript
Angular8 简单表单验证的实现示例
2020/06/03 Javascript
Vue实现指令式动态追加小球动画组件的步骤
2020/12/18 Vue.js
[02:49]2014DOTA2电竞也是体育项目! 势要把荣誉带回中国!
2014/07/20 DOTA
利用Python批量压缩png方法实例(支持过滤个别文件与文件夹)
2017/07/30 Python
python+matplotlib实现礼盒柱状图实例代码
2018/01/16 Python
解决Python网页爬虫之中文乱码问题
2018/05/11 Python
Python 通过requests实现腾讯新闻抓取爬虫的方法
2019/02/22 Python
通过python连接Linux命令行代码实例
2020/02/18 Python
解决keras,val_categorical_accuracy:,0.0000e+00问题
2020/07/02 Python
如何通过python检查文件是否被占用
2020/12/18 Python
HTML5通过调用canvas对象的getContext()方法来获取绘图环境
2014/06/23 HTML / CSS
Holiday Inn中国官网:IHG旗下假日酒店预订
2018/04/08 全球购物
荟萃全球保健品:维他购
2018/05/09 全球购物
命名空间(namespace)和程序集(Assembly)有什么区别
2015/09/25 面试题
网页美工求职信范文
2014/04/17 职场文书
画展邀请函
2015/01/31 职场文书
会计工作态度自我评价
2015/03/06 职场文书
2015年党建工作总结
2015/03/30 职场文书
Nginx服务器添加Systemd自定义服务过程解析
2021/03/31 Servers
PHP正则表达式之RCEService回溯
2022/04/11 PHP
Linux下使用C语言代码搭建一个简单的HTTP服务器
2022/04/13 Servers