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实现网络测试的脚本分享
May 26 Python
基于python socketserver框架全面解析
Sep 21 Python
windows下添加Python环境变量的方法汇总
May 14 Python
对Tensorflow中权值和feature map的可视化详解
Jun 14 Python
Python for循环中的陷阱详解
Jul 13 Python
Python多进程池 multiprocessing Pool用法示例
Sep 07 Python
Pytorch实现的手写数字mnist识别功能完整示例
Dec 13 Python
pytorch 修改预训练model实例
Jan 18 Python
Python xlrd模块导入过程及常用操作
Jun 10 Python
Python魔术方法专题
Jun 19 Python
python使用smtplib模块发送邮件
Dec 17 Python
python Timer 类使用介绍
Dec 28 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绘制一条直线的方法
2015/01/24 PHP
PHP使用ffmpeg给视频增加字幕显示的方法
2015/03/12 PHP
php遍历类中包含的所有元素的方法
2015/05/12 PHP
PHP生成唯一订单号
2015/07/05 PHP
php关键字仅替换一次的实现函数
2015/10/29 PHP
laravel 输出最后执行sql 附:whereIn的使用方法
2019/10/10 PHP
PHP copy函数使用案例代码解析
2020/09/01 PHP
arguments对象
2006/11/20 Javascript
jquery HotKeys轻松搞定键盘事件代码
2008/08/30 Javascript
模仿jQuery each函数的链式调用
2009/07/22 Javascript
读JavaScript DOM编程艺术笔记
2011/11/15 Javascript
jquery实现人性化的有选择性禁用鼠标右键
2014/06/30 Javascript
Base64(二进制)图片编码解析及在各种浏览器的兼容性处理
2017/02/09 Javascript
angular.js指令中的controller、compile与link函数的不同之处
2017/05/10 Javascript
在vue中使用css modules替代scroped的方法
2018/03/10 Javascript
vue+iview动态渲染表格详解
2019/03/19 Javascript
使用Easyui实现查询条件的后端传递并自动刷新表格的两种方法
2019/09/09 Javascript
Python判断文件和文件夹是否存在的方法
2015/05/21 Python
请不要重复犯我在学习Python和Linux系统上的错误
2016/12/12 Python
python中异常捕获方法详解
2017/03/03 Python
Python中类的初始化特殊方法
2017/12/01 Python
《与孩子一起学编程》python自测题
2018/05/27 Python
Python使用random.shuffle()打乱列表顺序的方法
2018/11/08 Python
Python3.6.x中内置函数总结及讲解
2019/02/22 Python
英国舒适型鞋履品牌:FitFlop
2017/05/17 全球购物
比利时买床:Beter Bed
2017/12/06 全球购物
交通专业个人自荐信格式
2013/09/23 职场文书
大型晚会策划方案
2014/02/06 职场文书
保护黄河倡议书
2014/05/16 职场文书
结对共建工作方案
2014/06/02 职场文书
优秀共产党员事迹材料
2014/12/18 职场文书
兼职安全员岗位职责
2015/02/15 职场文书
四风之害观后感
2015/06/09 职场文书
2015秋季开学演讲稿范文
2015/07/16 职场文书
CSS3常见动画的实现方式
2021/04/14 HTML / CSS
教你使用TensorFlow2识别验证码
2021/06/11 Python