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实现TCP/IP协议下的端口转发及重定向示例
Jun 14 Python
Python简单实现安全开关文件的两种方式
Sep 19 Python
Python迭代和迭代器详解
Nov 10 Python
python贪婪匹配以及多行匹配的实例讲解
Apr 19 Python
Python 3.x 判断 dict 是否包含某键值的实例讲解
Jul 06 Python
python 实现语音聊天机器人的示例代码
Dec 02 Python
python简单鼠标自动点击某区域的实例
Jun 25 Python
基于python的列表list和集合set操作
Nov 24 Python
OpenCV+Python--RGB转HSI的实现
Nov 27 Python
Python计算指定日期是今年的第几天(三种方法)
Mar 26 Python
Python日志器使用方法及原理解析
Sep 27 Python
Python 2.6.6升级到Python2.7.15的详细步骤
Dec 14 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中日期加减法运算实现代码
2011/12/08 PHP
Yii使用Captcha验证码的方法
2015/12/28 PHP
POST一个JSON格式的数据给Restful服务实例详解
2017/04/07 PHP
PHP获取访问设备信息的方法示例
2019/02/20 PHP
PHP反射原理与用法深入分析
2019/09/28 PHP
javascript实现上传图片前的预览(TX的面试题)
2007/08/20 Javascript
setTimeout与setInterval在不同浏览器下的差异
2010/01/24 Javascript
jquery $.fn $.fx是什么意思有什么用
2013/11/04 Javascript
jQuery+PHP打造滑动开关效果
2014/12/16 Javascript
jQuery实现长按按钮触发事件的方法
2015/02/02 Javascript
JavaScript比较两个对象是否相等的方法
2015/02/06 Javascript
JS实现超简单的鼠标拖动效果
2015/11/02 Javascript
JQuery移动页面开发之屏幕方向改变与滚屏的实现
2015/12/03 Javascript
javascript实现简易计算器
2017/02/01 Javascript
浅谈关于.vue文件中style的scoped属性
2017/08/19 Javascript
vue双花括号的使用方法 附练习题
2017/11/07 Javascript
在vue项目中使用Nprogress.js进度条的方法
2018/01/31 Javascript
jQuery实现的两种简单弹窗效果示例
2018/04/18 jQuery
vue.js实现格式化时间并每秒更新显示功能示例
2018/07/07 Javascript
html+jQuery实现拖动滑块图片拼图验证码插件【移动端适用】
2019/09/10 jQuery
基于Layui自定义模块的使用方法详解
2019/09/14 Javascript
[01:14:55]EG vs Spirit Supermajor 败者组 BO3 第三场 6.4
2018/06/05 DOTA
浅析Python中的多条件排序实现
2016/06/07 Python
使用python爬虫实现网络股票信息爬取的demo
2018/01/05 Python
PyTorch的深度学习入门之PyTorch安装和配置
2019/06/27 Python
Django 后台获取文件列表 InMemoryUploadedFile的例子
2019/08/07 Python
PyCharm无法识别PyQt5的2种解决方法,ModuleNotFoundError: No module named 'pyqt5'
2020/02/17 Python
解决python执行较大excel文件openpyxl慢问题
2020/05/15 Python
解决python运行效率不高的问题
2020/07/20 Python
python进度条显示之tqmd模块
2020/08/22 Python
利用CSS3的border-radius绘制太极及爱心图案示例
2016/05/17 HTML / CSS
html5-Canvas可以在web中绘制各种图形
2012/12/26 HTML / CSS
师说教学反思
2014/02/07 职场文书
小学端午节活动方案
2014/03/13 职场文书
Nginx实现负载均衡的项目实践
2022/03/18 Servers
Nginx+Tomcat负载均衡多实例详解
2022/04/11 Servers