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 相关文章推荐
基于wxpython实现的windows GUI程序实例
May 30 Python
Python Web框架Tornado运行和部署
Oct 19 Python
PyGame贪吃蛇的实现代码示例
Nov 21 Python
Python3安装Pillow与PIL的方法
Apr 03 Python
Django处理多用户类型的方法介绍
May 18 Python
使用Python OpenCV为CNN增加图像样本的实现
Jun 10 Python
基于python cut和qcut的用法及区别详解
Nov 22 Python
使用Rasterio读取栅格数据的实例讲解
Nov 26 Python
python [:3] 实现提取数组中的数
Nov 27 Python
django的autoreload机制实现
Jun 03 Python
Java如何基于wsimport调用wcf接口
Jun 17 Python
用Python爬取LOL所有的英雄信息以及英雄皮肤的示例代码
Jul 13 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笔记 字符串处理
2010/10/19 PHP
php 求质素(素数) 的实现代码
2011/04/12 PHP
php shell超强免杀、减少体积工具实现代码
2012/10/16 PHP
用Json实现PHP与JavaScript间数据交换的方法详解
2013/06/20 PHP
PHP中的日期加减方法示例
2014/08/21 PHP
php计算整个目录大小的方法
2015/06/01 PHP
给WordPress中的留言加上楼层号的PHP代码实例
2015/12/14 PHP
WordPress中注册菜单与调用菜单的方法详解
2015/12/18 PHP
CI框架中redis缓存相关操作文件示例代码
2016/05/17 PHP
laravel 事件/监听器实例代码
2019/04/12 PHP
TP5框架页面跳转样式操作示例
2020/04/05 PHP
jQuery 全选效果实现代码
2009/03/23 Javascript
js中flexible.js实现淘宝弹性布局方案
2020/06/23 Javascript
基于javascript实现数字英文验证码
2017/01/25 Javascript
JS限制输入框输入的实现代码
2018/07/02 Javascript
Vue源码解读之Component组件注册的实现
2018/08/24 Javascript
json 带斜杠时如何解析的实现
2019/08/12 Javascript
微信小程序通过js实现瀑布流布局详解
2019/08/28 Javascript
vue项目在线上服务器访问失败原因分析
2020/08/14 Javascript
Python通过命令开启http.server服务器的方法
2017/11/04 Python
使用python opencv对目录下图片进行去重的方法
2019/01/12 Python
Python爬虫库BeautifulSoup的介绍与简单使用实例
2020/01/25 Python
python中安装django模块的方法
2020/03/12 Python
浅析python 字典嵌套
2020/09/29 Python
如何用 Python 处理不平衡数据集
2021/01/04 Python
深入理解Python变量的数据类型和存储
2021/02/01 Python
Yves Rocher伊夫·黎雪美国官网:法国始创植物美肌1959
2019/01/09 全球购物
英国领先的鞋类零售商和顶级品牌的官方零售商:Wynsors
2020/02/17 全球购物
医学生自我鉴定范文
2013/11/08 职场文书
水果超市创业计划书
2014/01/27 职场文书
教师师德演讲稿
2014/05/06 职场文书
夫妻分居协议书范文
2014/11/26 职场文书
参观邀请函范文
2015/02/02 职场文书
2015年妇幼保健工作总结
2015/05/19 职场文书
2016年“6.26”禁毒宣传月系列活动总结
2016/04/05 职场文书
详解SQL的窗口函数
2022/04/21 Oracle