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语言实现将图片转化为html页面
Dec 06 Python
Python 12306抢火车票脚本 Python京东抢手机脚本
Feb 06 Python
Python使用sort和class实现的多级排序功能示例
Aug 15 Python
python3爬虫怎样构建请求header
Dec 23 Python
python requests 库请求带有文件参数的接口实例
Jan 03 Python
python删除文件夹下相同文件和无法打开的图片
Jul 16 Python
django2笔记之路由path语法的实现
Jul 17 Python
Python3网络爬虫开发实战之极验滑动验证码的识别
Aug 02 Python
详解Pycharm与anaconda安装配置指南
Aug 25 Python
关于django python manage.py startapp 应用名出错异常原因解析
Dec 15 Python
python中time包实例详解
Feb 02 Python
Python3.9.1中使用split()的处理方法(推荐)
Feb 07 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购物车实现代码
2011/10/10 PHP
php计算几分钟前、几小时前、几天前的几个函数、类分享
2014/04/09 PHP
php网站被挂木马后的修复方法总结
2014/11/06 PHP
PHP析构函数destruct与垃圾回收机制的讲解
2019/03/22 PHP
Extjs TimeField 显示正常时间格式的代码
2011/06/28 Javascript
jQuery基本选择器选择元素使用介绍
2013/04/18 Javascript
js对象与打印对象分析比较
2013/04/23 Javascript
使用JavaScript 实现对象 匀速/变速运动的方法
2013/05/08 Javascript
jquery导航制件jquery鼠标经过变色效果示例
2013/12/05 Javascript
AngularJs解决跨域问题案例详解(简单方法)
2016/05/19 Javascript
关于JSON.parse(),JSON.stringify(),jQuery.parseJSON()的用法
2016/06/30 Javascript
Javascript中字符串replace方法的第二个参数探究
2016/12/05 Javascript
seajs中模块依赖的加载处理实例分析
2017/10/10 Javascript
JS实现的数组去除重复数据算法小结
2017/11/17 Javascript
vue axios请求拦截实例代码
2018/03/29 Javascript
[02:10]DOTA2亚洲邀请赛 EG战队出场宣传片
2015/02/07 DOTA
[01:01:24]LGD vs Fnatic 2018国际邀请赛小组赛BO2 第一场 8.18
2018/08/19 DOTA
[38:54]完美世界DOTA2联赛PWL S2 Rebirth vs LBZS 第一场 11.28
2020/12/01 DOTA
[02:50]【扭转乾坤,只此一招】DOTA2永雾林渊版本开启新篇章
2020/12/22 DOTA
python 生成目录树及显示文件大小的代码
2009/07/23 Python
实践Vim配置python开发环境
2018/07/02 Python
利用python如何在前程无忧高效投递简历
2019/05/07 Python
Python generator生成器和yield表达式详解
2019/08/08 Python
python 基于dlib库的人脸检测的实现
2019/11/08 Python
Python telnet登陆功能实现代码
2020/04/16 Python
python 解决pycharm运行py文件只有unittest选项的问题
2020/09/01 Python
GAP美国官网:美国休闲时尚品牌
2016/08/26 全球购物
中专生自荐信
2013/10/12 职场文书
英文版区域经理求职信
2013/10/23 职场文书
初中女生自我鉴定
2013/12/19 职场文书
工地门卫岗位职责
2013/12/30 职场文书
机关搬迁方案
2014/05/18 职场文书
营销计划书
2015/01/17 职场文书
物业接待员岗位职责
2015/04/15 职场文书
答辩状格式范本
2015/05/22 职场文书
2016年优秀党员教师先进事迹材料
2016/02/29 职场文书