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中使用PIPE操作Linux管道
Feb 04 Python
举例讲解Python中的迭代器、生成器与列表解析用法
Mar 20 Python
Python中的取模运算方法
Nov 10 Python
pyQt5实时刷新界面的示例
Jun 25 Python
Python-接口开发入门解析
Aug 01 Python
Python3使用腾讯云文字识别(腾讯OCR)提取图片中的文字内容实例详解
Feb 18 Python
Python中求对数方法总结
Mar 10 Python
Django更新models数据库结构步骤
Apr 01 Python
python由已知数组快速生成新数组的方法
Apr 08 Python
keras 获取某层输出 获取复用层的多次输出实例
May 23 Python
python利用递归方法实现求集合的幂集
Sep 07 Python
用python制作个视频下载器
Feb 01 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
中国广播史趣谈 — 几个历史第一次
2021/03/01 无线电
PHP中几个常用的魔术常量
2012/02/23 PHP
ThinkPHP3.1新特性之G方法的使用
2014/06/19 PHP
PHP实现递归复制整个文件夹的类实例
2015/08/03 PHP
PHP调用微博接口实现微博登录的方法示例
2018/09/22 PHP
JQuery 写的个性导航菜单
2009/12/24 Javascript
js实现的真正的iframe高度自适应(兼容IE,FF,Opera)
2010/03/07 Javascript
JavaScript小技巧 2.5 则
2010/09/12 Javascript
javascript dom追加内容实现示例
2013/09/21 Javascript
JavaScript拆分字符串时产生空字符的解决方案
2014/09/26 Javascript
BAT及各大互联网公司2014前端笔试面试题--JavaScript篇
2014/10/29 Javascript
浅谈JavaScript的Polymer框架中的behaviors对象
2015/07/29 Javascript
jQuery form插件之formDdata参数校验表单及验证后提交
2016/01/23 Javascript
TypeOf这些知识点你了解吗
2016/02/21 Javascript
深入理解js generator数据类型
2016/08/16 Javascript
预防网页挂马的方法总结
2016/11/03 Javascript
AngularJS操作键值对象类似java的hashmap(填坑小结)
2016/11/12 Javascript
单击按钮发送验证码,出现倒计时的简单实例
2017/03/17 Javascript
Bootstrap实现各种进度条样式详解
2017/04/13 Javascript
Angular4实现动态添加删除表单输入框功能
2017/08/11 Javascript
移动web开发之touch事件实例详解
2018/01/17 Javascript
element日历calendar组件上月、今天、下月、日历块点击事件及模板源码
2020/07/27 Javascript
python 循环遍历字典元素的简单方法
2016/09/11 Python
Python的iOS自动化打包实例代码
2018/11/22 Python
Python 给屏幕打印信息加上颜色的实现方法
2019/04/24 Python
Roxy美国官网:澳大利亚冲浪、滑雪健身品牌
2016/07/30 全球购物
巴西24小时在线药房:Droga Raia
2020/05/12 全球购物
澳大利亚第一旅行车和房车配件店:Caravan RV Camping
2020/12/26 全球购物
求职自荐书范文
2013/12/04 职场文书
四下基层实施方案
2014/03/28 职场文书
投资意向书范本
2014/04/01 职场文书
家长意见和建议怎么写
2015/06/04 职场文书
Jupyter notebook 不自动弹出网页的解决方案
2021/05/21 Python
Python基础之条件语句详解
2021/06/16 Python
使用SQL实现车流量的计算的示例代码
2022/02/28 SQL Server
海弦WR-800F
2022/04/05 无线电