python实现代码统计程序


Posted in Python onSeptember 19, 2019

本文实例为大家分享了python实现代码统计程序的具体代码,供大家参考,具体内容如下

# encoding="utf-8"

"""
统计代码行数
"""

import sys
import os

def count_file_line(path):
 """统计文件的有效行数"""
 countLine = 0
 # 设置一个标志位,当遇到以"""或者'''开头或者结尾的时候,置为False
 flag = True

 # 使用utf-8格式的编码方式读取文件,如果读取失败,将使用gbk编码方式读取文件
 try:
 fp = open(path, "r", encoding="utf-8")
 encoding_type = "utf-8"
 fp.close()
 except:
 encoding_type = "gbk"

 with open(path, "r", encoding=encoding_type) as fp:
 for line in fp:
  # 空行不统计
  if line.strip():
  line = line.strip()
  # 注意下面的这两个elif必须要前面,这样子当('"""')结束之后及时将flag置为True
  if line.endswith('"""') and flag == False:
   flag = True
   continue
  if line.endswith("'''") and flag == False:
   flag = True
   continue
  if flag == False:
   continue
  if line.startswith("#!") or line.startswith("#-*-") or line.startswith("# encoding"):
   countLine += 1
  # 如果以“#”号开头的,不统计
  elif line.startswith("#"):
   continue
  # 如果同时以("'''")或者('"""')开头或者结尾(比如:"""aaa"""),那么不统计
  elif line.startswith('"""') and line.endswith('"""') and line != '"""':
   continue
  elif line.startswith("'''") and line.endswith("'''") and line != "'''":
   continue
  # 如果以("'''")或者('"""')开头或者结尾(比如:aaa"""或者"""bbb),那么不统计
  # 注意下面的这两个elif必须要放后面
  elif line.startswith('"""') and flag == True:
   flag = False
   continue
  elif line.startswith("'''") and flag == True:
   flag = False
   continue
  else:
   countLine += 1
 return countLine

def count_codes(path,file_types=[]):
 """统计所有文件代码行"""
 # 判断path是目录还是文件,如果是目录的话,遍历目录下所有的文件
 if not os.path.exists(path):
 print("您输入的路径不存在!")
 return 0
 countTotalLine = 0
 file_paths = {}
 if os.path.isdir(path):
 for root,dirs,files in os.walk(path):
  for name in files:
  if not file_types:
   file_types = ["txt","py"]
   # print(file_types)
  if os.path.splitext(name)[1][1:] in file_types:
   file_path = os.path.normpath(os.path.join(root,name))
   # print(file_path)
   file_lines = count_file_line(file_path)
   countTotalLine += file_lines
   file_paths[file_path] = file_lines
 else:
 if not file_types:
  file_types = ["txt","py"]
 if os.path.splitext(path)[1][1:] in file_types:
  countTotalLine = count_file_line(path)
  file_paths[path] = count_file_line(path)

 return countTotalLine,file_paths


if __name__ == "__main__":
 # 打印出命令行输入的参数
 # print(sys.argv)
 if len(sys.argv) < 2:
 print("请输入路径!")
 sys.exit()
 path = sys.argv[1]
 # print(path)
 file_types = sys.argv[2:]
 # print(file_types)
 print(count_codes(path,file_types))

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python实现矩阵乘法的方法
Jun 28 Python
python中nan与inf转为特定数字方法示例
May 11 Python
Django使用httpresponse返回用户头像实例代码
Jan 26 Python
python验证码识别教程之利用滴水算法分割图片
Jun 05 Python
Python多项式回归的实现方法
Mar 11 Python
解决pycharm下os.system执行命令返回有中文乱码的问题
Jul 07 Python
讲解Python3中NumPy数组寻找特定元素下标的两种方法
Aug 04 Python
如何获取Python简单for循环索引
Nov 21 Python
如何基于Python + requests实现发送HTTP请求
Jan 13 Python
python中列表的含义及用法
May 26 Python
Python使用jpype模块调用jar包过程解析
Jul 29 Python
matplotlib之pyplot模块实现添加子图subplot的使用
Apr 25 Python
python tkinter图形界面代码统计工具(更新)
Sep 18 #Python
python3获取url文件大小示例代码
Sep 18 #Python
弄懂这56个Python使用技巧(轻松掌握Python高效开发)
Sep 18 #Python
python3使用GUI统计代码量
Sep 18 #Python
django中的图片验证码功能
Sep 18 #Python
python tkinter图形界面代码统计工具
Sep 18 #Python
Python自动生成代码 使用tkinter图形化操作并生成代码框架
Sep 18 #Python
You might like
MVC模式的PHP实现
2006/10/09 PHP
php购物车实现代码
2011/10/10 PHP
深入理解PHP几个算法:PHP冒泡、PHP二分法、PHP求素数、PHP乘法表
2013/06/06 PHP
php使用Jpgraph绘制简单X-Y坐标图的方法
2015/06/10 PHP
php实现面包屑导航例子分享
2015/12/19 PHP
PHP实现的简单分页类及用法示例
2016/05/06 PHP
PHP编写的图片验证码类文件分享
2016/06/06 PHP
不要小看注释掉的JS 引起的安全问题
2008/12/27 Javascript
两种方法实现文本框输入内容提示消失
2013/03/17 Javascript
jquery ztree实现下拉树形框使用到了json数据
2014/05/14 Javascript
再谈javascript原型继承
2014/11/10 Javascript
js实现鼠标悬停图片上时滚动文字说明的方法
2015/02/17 Javascript
jQuery插件EnPlaceholder实现输入框提示文字
2015/06/05 Javascript
jQuery+jsp实现省市县三级联动效果(附源码)
2015/12/03 Javascript
JS Array.slice 截取数组的实现方法
2016/01/02 Javascript
JS实现图片上传预览功能
2016/11/21 Javascript
vue项目实战总结篇
2018/02/11 Javascript
Vue+Koa2+mongoose写一个像素绘板的实现方法
2019/09/10 Javascript
vue实例的选项总结
2020/06/09 Javascript
纯js+css实现在线时钟
2020/08/18 Javascript
python批量修改文件后缀示例代码分享
2013/12/24 Python
Python和Perl绘制中国北京跑步地图的方法
2016/03/03 Python
深入理解Python装饰器
2016/07/27 Python
Python3.7中安装openCV库的方法
2018/07/11 Python
基于Python在MacOS上安装robotframework-ride
2018/12/28 Python
对numpy下的轴交换transpose和swapaxes的示例解读
2019/06/26 Python
Python pandas用法最全整理
2019/08/04 Python
tensorflow模型文件(ckpt)转pb文件的方法(不知道输出节点名)
2020/04/22 Python
Keras: model实现固定部分layer,训练部分layer操作
2020/06/28 Python
Python模拟登录和登录跳转的参考示例
2020/10/30 Python
如何写好优秀的创业计划书
2014/01/30 职场文书
2014年党员公开承诺书范文
2014/03/28 职场文书
法院先进个人事迹材料
2014/05/04 职场文书
旅游文化节策划方案
2014/06/06 职场文书
村党支部群众路线教育实践活动对照检查材料
2014/09/26 职场文书
史上最牛辞职信
2015/05/13 职场文书