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字符串排序方法
Aug 29 Python
Python实现遍历数据库并获取key的值
May 17 Python
详解在Python程序中解析并修改XML内容的方法
Nov 16 Python
python入门前的第一课 python怎样入门
Mar 06 Python
Python实现的网页截图功能【PyQt4与selenium组件】
Jul 12 Python
python实现简单图片物体标注工具
Mar 18 Python
Django学习笔记之为Model添加Action
Apr 30 Python
Python3 批量扫描端口的例子
Jul 25 Python
python的移位操作实现详解
Aug 21 Python
django-csrf使用和禁用方式
Mar 13 Python
Python图像识别+KNN求解数独的实现
Nov 13 Python
Python机器学习之KNN近邻算法
May 14 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一句话cmdshell新型 (非一句话木马)
2009/04/18 PHP
PHP高级对象构建 多个构造函数的使用
2012/02/05 PHP
完美解决令人抓狂的zend studio 7代码提示(content Assist)速度慢的问题
2013/06/20 PHP
php用ini_get获取php.ini里变量值的方法
2015/03/04 PHP
js setattribute批量设置css样式
2009/11/26 Javascript
12种不宜使用的Javascript语法整理
2013/11/04 Javascript
Jquery中offset()和position()的区别分析
2015/02/05 Javascript
js实现模拟银行卡账号输入显示效果
2015/11/18 Javascript
深入理解JavaScript中为什么string可以拥有方法
2016/05/24 Javascript
JS实现图片上传预览功能
2016/11/21 Javascript
微信小程序 数组中的push与concat的区别
2017/01/05 Javascript
鼠标经过出现气泡框的简单实例
2017/03/17 Javascript
H5上传本地图片并预览功能
2017/05/08 Javascript
浅谈angular.copy() 深拷贝
2017/09/14 Javascript
Vue修改mint-ui默认样式的方法
2018/02/03 Javascript
vue打包之后生成一个配置文件修改接口的方法
2018/12/09 Javascript
微信小程序实现随机验证码功能
2018/12/20 Javascript
Python网络爬虫项目:内容提取器的定义
2016/10/25 Python
使用sklearn之LabelEncoder将Label标准化的方法
2018/07/11 Python
python tkinter界面居中显示的方法
2018/10/11 Python
python 调用API接口 获取和解析 Json数据
2020/09/28 Python
python飞机大战游戏实例讲解
2020/12/04 Python
GUESS德国官网:美国牛仔服装品牌
2017/02/14 全球购物
文件中有一组整数,要求排序后输出到另一个文件中
2012/01/04 面试题
学院书画协会部门岗位职责
2013/12/01 职场文书
毕业生自荐信
2013/12/14 职场文书
教师开学感言
2014/02/14 职场文书
三分钟英语演讲稿
2014/04/24 职场文书
白岩松演讲
2014/05/21 职场文书
防火标语大全
2014/10/06 职场文书
个人查摆剖析材料
2014/10/16 职场文书
2015年小学二年级班主任工作总结
2015/05/21 职场文书
2016大学迎新晚会开场白
2015/11/24 职场文书
银行服务理念口号
2015/12/25 职场文书
MySQL update set 和 and的区别
2021/05/08 MySQL
一文搞懂php的垃圾回收机制
2021/06/18 PHP