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入门篇之对象类型
Oct 17 Python
python实现超简单端口转发的方法
Mar 13 Python
利用Python实现命令行版的火车票查看器
Aug 05 Python
python使用opencv进行人脸识别
Apr 07 Python
Python进程间通信之共享内存详解
Oct 30 Python
Python算法之图的遍历
Nov 16 Python
Python pip替换为阿里源的方法步骤
Jul 02 Python
将python依赖包打包成window下可执行文件bat方式
Dec 26 Python
django在保存图像的同时压缩图像示例代码详解
Feb 11 Python
Python爬虫获取页面所有URL链接过程详解
Jun 04 Python
torchxrayvision包安装过程(附pytorch1.6cpu版安装)
Aug 26 Python
scrapy-splash简单使用详解
Feb 21 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
php 静态变量的初始化
2009/11/15 PHP
for循环连续求和、九九乘法表代码
2012/02/20 PHP
记录mysql性能查询过程的使用方法
2013/05/02 PHP
对PHP新手的一些建议(PHP学习经验总结)
2014/08/20 PHP
又十个超级有用的PHP代码片段
2015/09/24 PHP
php微信公众号开发(3)php实现简单微信文本通讯
2016/12/15 PHP
PHP实现动态添加XML中数据的方法
2018/03/30 PHP
javascript 精粹笔记
2010/05/09 Javascript
JS控制阿拉伯数字转为中文大写示例代码
2013/09/04 Javascript
jquery放大镜效果超漂亮噢
2013/11/15 Javascript
JS实现的自定义右键菜单实例二则
2015/09/01 Javascript
JavaScript实现的多个图片广告交替显示效果代码
2015/09/04 Javascript
vue-router路由简单案例介绍
2017/02/21 Javascript
Js自动截取字符串长度,添加省略号(……)的实现方法
2017/03/06 Javascript
JS实现瀑布流布局
2017/10/21 Javascript
基于vue 开发中出现警告问题去除方法
2018/01/25 Javascript
详解webpack之图片引入-增强的file-loader:url-loader
2018/10/08 Javascript
微信小游戏之使用three.js 绘制一个旋转的三角形
2019/06/10 Javascript
JS为什么说async/await是generator的语法糖详解
2019/07/11 Javascript
js实现随机点名功能
2020/12/23 Javascript
[54:30]Liquid vs Newbee 2019国际邀请赛小组赛 BO2 第二场 8.15
2019/08/16 DOTA
python字典排序实例详解
2015/05/20 Python
Python判断文本中消息重复次数的方法
2016/04/27 Python
深入理解Django自定义信号(signals)
2018/10/15 Python
python 监测内存和cpu的使用率实例
2019/11/28 Python
法国娇韵诗官方旗舰店:Clarins是来自法国的天然护肤品牌
2018/06/30 全球购物
西部世纪.net笔试题面试题
2014/04/03 面试题
入党积极分子介绍信
2014/01/17 职场文书
教师网络培训感言
2014/03/09 职场文书
《山谷中的谜底》教学反思
2014/04/26 职场文书
法制宣传标语
2014/06/23 职场文书
股东出资证明书(正规版)
2014/09/24 职场文书
2014年公务员个人工作总结
2014/11/22 职场文书
房地产销售主管岗位职责
2015/02/13 职场文书
Python如何用re模块实现简易tokenizer
2022/05/02 Python
openGauss数据库JDBC环境连接配置的详细过程(Eclipse)
2022/06/01 Java/Android