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中threading超线程用法实例分析
May 16 Python
通过数据库对Django进行删除字段和删除模型的操作
Jul 21 Python
python学习必备知识汇总
Sep 08 Python
python爬虫获取百度首页内容教学
Dec 23 Python
python腾讯语音合成实现过程解析
Aug 01 Python
python基于socket实现的UDP及TCP通讯功能示例
Nov 01 Python
python读取ini配置文件过程示范
Dec 23 Python
windows下Pycharm安装opencv的多种方法
Mar 05 Python
python多线程实现同时执行两个while循环的操作
May 02 Python
安装并免费使用Pycharm专业版(学生/教师)
Sep 24 Python
python连接手机自动搜集蚂蚁森林能量的实现代码
Feb 24 Python
python神经网络学习 使用Keras进行简单分类
May 04 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
yii2.0实现验证用户名与邮箱功能
2015/12/22 PHP
highchart数据源纵轴json内的值必须是int(详解)
2017/02/20 PHP
laravel 如何实现引入自己的函数或类库
2019/10/15 PHP
goto语法在PHP中的使用教程
2020/09/17 PHP
dojo 之基础篇
2007/03/24 Javascript
js动态添加onload、onresize、onscroll事件(另类方法)
2012/12/26 Javascript
js保留两位小数使用toFixed实现
2013/07/29 Javascript
jQuery知识点整理
2015/01/30 Javascript
莱鸟介绍window.print()方法
2016/01/06 Javascript
AngularJS压缩JS技巧分析
2016/11/08 Javascript
Web前端框架bootstrap实战【第一次接触使用】
2016/12/28 Javascript
jQuery插件实现弹性运动完整示例
2018/07/07 jQuery
nodejs高大上的部署方式(PM2)
2018/09/11 NodeJs
vue进入页面时不在顶部,检测滚动返回顶部按钮问题及解决方法
2019/10/30 Javascript
微信小程序新闻网站详情页实例代码
2020/01/10 Javascript
[49:35]KG vs SECRET 2019国际邀请赛小组赛 BO2 第一场 8.16
2019/08/19 DOTA
使用Eclipse如何开发python脚本
2018/04/11 Python
破解安装Pycharm的方法
2018/10/19 Python
Python:合并两个numpy矩阵的实现
2019/12/02 Python
Python使用urllib模块对URL网址中的中文编码与解码实例详解
2020/02/18 Python
JD Sports比利时官网:英国领先的运动鞋和运动服饰零售商
2018/10/10 全球购物
什么是Linux虚拟文件系统VFS
2012/01/31 面试题
电大物流学生的自我评价
2013/10/25 职场文书
创伤外科专业推荐信范文
2013/11/19 职场文书
英文自荐信常用句子
2014/03/26 职场文书
关于护士节的演讲稿
2014/05/26 职场文书
领导班子党的群众路线教育实践活动对照检查材料
2014/09/25 职场文书
“向国旗敬礼”主题班会活动设计方案
2014/09/27 职场文书
工伤事故证明
2014/10/20 职场文书
2014年卫生院工作总结
2014/12/03 职场文书
义诊活动总结
2015/02/04 职场文书
妈妈再爱我一次观后感
2015/06/08 职场文书
工作年限证明范本
2015/06/15 职场文书
原生JS中应该禁止出现的写法
2021/05/05 Javascript
带你学习MySQL执行计划
2021/05/31 MySQL
SpringBoot读取Resource下文件的4种方法
2021/07/02 Java/Android