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中的json总结
Oct 11 Python
Python实现SQL注入检测插件实例代码
Feb 02 Python
python实现列表的排序方法分享
Jul 01 Python
对Python获取屏幕截图的4种方法详解
Aug 27 Python
python 实现目录复制的三种小结
Dec 04 Python
如何基于pythonnet调用halcon脚本
Jan 20 Python
Python实现子类调用父类的初始化实例
Mar 12 Python
自定义Django Form中choicefield下拉菜单选取数据库内容实例
Mar 13 Python
彻底搞懂python 迭代器和生成器
Sep 07 Python
python实现二分查找算法
Sep 18 Python
详解基于Scrapy的IP代理池搭建
Sep 29 Python
关于python中remove的一些坑小结
Jan 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
PHP 定界符 使用技巧
2009/06/14 PHP
php程序员应具有的7种能力小结
2014/11/27 PHP
PHP SPL标准库之文件操作(SplFileInfo和SplFileObject)实例
2015/05/11 PHP
理解Javascript_08_函数对象
2010/10/15 Javascript
javascript中文本框中输入法切换的问题
2013/12/10 Javascript
nodejs中实现阻塞实例
2015/03/24 NodeJs
分离与继承的思想实现图片上传后的预览功能:ImageUploadView
2016/04/07 Javascript
jQuery操作iframe中js函数的方法小结
2016/07/06 Javascript
webpack配置导致字体图标无法显示的解决方法
2018/03/06 Javascript
layui table 参数设置方法
2018/08/14 Javascript
微信小程序如何获取用户收货地址
2018/11/27 Javascript
微信小程序在地图选择地址并返回经纬度简单示例
2018/12/03 Javascript
超好用的jQuery分页插件jpaginate用法示例【附源码下载】
2018/12/06 jQuery
js实现继承的方法及优缺点总结
2019/05/08 Javascript
浅谈vue 锚点指令v-anchor的使用
2019/11/13 Javascript
Python实现计算文件夹下.h和.cpp文件的总行数
2015/04/23 Python
python django 访问静态文件出现404或500错误
2017/01/20 Python
python实现word 2007文档转换为pdf文件
2018/03/15 Python
flask中的wtforms使用方法
2018/07/21 Python
浅谈python中get pass用法
2019/03/19 Python
django queryset相加和筛选教程
2020/05/18 Python
python实现磁盘日志清理的示例
2020/11/05 Python
python解压zip包中文乱码解决方法
2020/11/27 Python
HTML5标签嵌套规则详解【必看】
2016/04/26 HTML / CSS
你的创业计划书怎样才能打动风投
2014/02/06 职场文书
委托协议书范本
2014/04/22 职场文书
文明村镇申报材料
2014/05/06 职场文书
员工保密协议书
2014/09/27 职场文书
档案管理员岗位职责
2015/02/12 职场文书
学术会议通知
2015/04/15 职场文书
初中政治教师教学反思
2016/02/23 职场文书
幽默导游词应该怎么写?
2019/08/26 职场文书
MySql开发之自动同步表结构
2021/05/28 MySQL
python中tkinter复选框使用操作
2021/11/11 Python
apache虚拟主机配置的三种方式(小结)
2022/07/23 Servers
uniapp引入支付宝原生扫码插件步骤详解
2022/07/23 Javascript