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使用PyGame绘制图像并保存为图片文件的方法
Apr 24 Python
解决python删除文件的权限错误问题
Apr 24 Python
Python 利用内置set函数对字符串和列表进行去重的方法
Jun 29 Python
Python实现的json文件读取及中文乱码显示问题解决方法
Aug 06 Python
简单了解Python matplotlib线的属性
Jun 29 Python
Python变量访问权限控制详解
Jun 29 Python
Pandas的read_csv函数参数分析详解
Jul 02 Python
python多线程实现代码(模拟银行服务操作流程)
Jan 13 Python
Django设置Postgresql的操作
May 14 Python
python 获取谷歌浏览器保存的密码
Jan 06 Python
python实现马丁策略回测3000只股票的实例代码
Jan 22 Python
详解Flask开发技巧之异常处理
Jun 15 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中好用但又容易忽略的小知识
2017/06/02 PHP
详解php用static方法的原因
2018/09/12 PHP
浅谈laravel 5.6 安装 windows上使用composer的安装过程
2019/10/18 PHP
jQuery 插件 将this下的div轮番显示
2009/04/09 Javascript
Js event事件在IE、FF兼容性问题
2011/01/01 Javascript
初学Jquery插件制作 在SageCRM的查询屏幕隐藏部分行的功能
2011/12/26 Javascript
javascript创建数组之联合数组的使用方法示例
2013/12/26 Javascript
jQuery中odd选择器的定义和用法
2014/12/23 Javascript
详解JavaScript函数
2015/12/01 Javascript
封装好的javascript前端分页插件pagination
2016/01/04 Javascript
JavaScript和jQuery制作光棒效果
2017/02/24 Javascript
用js屏蔽被http劫持的浮动广告实现方法
2017/08/10 Javascript
vue  directive定义全局和局部指令及指令简写
2018/11/20 Javascript
Layui表格监听行单双击事件讲解
2019/11/14 Javascript
python通过urllib2爬网页上种子下载示例
2014/02/24 Python
Python判断变量是否已经定义的方法
2014/08/18 Python
Python创建xml的方法
2015/03/10 Python
Python之re操作方法(详解)
2017/06/14 Python
python批量读取txt文件为DataFrame的方法
2018/04/03 Python
python的pstuil模块使用方法总结
2019/07/26 Python
PyQt5-QDateEdit的简单使用操作
2020/07/12 Python
Python3+selenium配置常见报错解决方案
2020/08/28 Python
浅谈python 类方法/静态方法
2020/09/18 Python
草莓网英国官网:Strawberrynet UK
2017/02/12 全球购物
黄色火烈鸟:De Gele Flamingo
2019/03/18 全球购物
工程项目经理岗位职责
2013/12/15 职场文书
快餐公司创业计划书
2014/04/29 职场文书
简单通用的简历自我评价
2014/09/21 职场文书
小学优秀教师材料
2014/12/15 职场文书
资料员岗位职责范本
2015/04/13 职场文书
夫妻吵架保证书
2015/05/08 职场文书
离婚民事起诉状
2015/08/03 职场文书
​(迎国庆)作文之我爱我的祖国
2019/09/19 职场文书
导游词之张家口
2019/12/13 职场文书
详细聊聊Oracle表碎片对性能有多大的影响
2022/03/19 Oracle
VUE之图片Base64编码使用ElementUI组件上传
2022/04/09 Vue.js