使用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 写了一个批量生成文件夹和批量重命名的工具
Python 写了一个批量生成文件夹和批量重命名的工具 目录 Python 写了一个批量生成文件夹和批量重命名的工具 演示 功能 1. 可以读取excel内容,使用excel单元格内容进行新建文件夹, ...
- 用Python写了一个postgresql函数,感觉很爽
用Python写了一个postgresql函数,感觉很爽 CREATE LANGUAGE plpythonu; postgresql函数 CREATE OR REPLACE FUNCTION myfu ...
- python 怎么启动一个外部命令程序, 并且不阻塞当前进程
http://www.myexception.cn/perl-python/1278887.html http://blog.chinaunix.net/uid-25979788-id-3081912 ...
- 我最近用Python写了一个算法,不需要写任何规则就能自动识别一个网页的内容
我最近用Python写了一个算法,不需要写任何规则就能自动识别一个网页的内容,目前测试了300多个新闻网站的新闻页,都能准确识别
- 用Racket语言写了一个万花筒的程序
用Racket语言写了一个万花筒的程序 来源:https://blog.csdn.net/chinazhangyong/article/details/79362394 https://github. ...
- 用python写个简单的小程序,编译成exe跑在win10上
每天的工作其实很无聊,早知道应该去IT公司闯荡的.最近的工作内容是每逢一个整点,从早7点到晚11点,去查一次客流数据,整理到表格中,上交给素未蒙面的上线,由他呈交领导查阅. 人的精力毕竟是有限的,所以 ...
- python写的一个集合
起因:原本打算用python写一个抢火车票的脚本.在那 期间遇见各种浏览器驱动失败的节奏....打算先缓一下 然后就去写集合了. 0x01 源码: # -*- coding:'utf-8' -*- # ...
- 用Python写的一个多线程机器人聊天程序
本人是从事php开发的, 近来想通过php实现即时通讯(兼容windows).后来发现实现起来特别麻烦, 就想到python.听说这家伙在什么地方都能发挥作用.所以想用python来做通讯模块...所 ...
- 分享下自己写的一个微信小程序请求远程数据加载到页面的代码
1 思路整理 就是页面加载完毕的时候 请求远程接口,然后把数据赋值给页面的变量 ,然后列表循环 2 js相关代码 我是改的 onload函数 /** * 生命周期函数--监听页面加载 */ on ...
随机推荐
- NOIP2018普及初赛解析
2018年第二十四届全国青少年信息学奥林匹克联赛初赛普及组真题解析 一.单项选择题 1. 以下哪一种设备属于输出设备:(D) A.扫描仪 _B.键盘C. 鼠标 _D. 打印机 解析:送分题,前三个都是 ...
- (一)Knockout 计算属性
1 Computed 首先,创建一个view model如下: <body> <p>The fullname is: <span data-bind="text ...
- 使用java的Calendar工具类获取到本月的第一天起始时间和最后一天结束时间。
1.使用java的Calendar工具类获取到本月的第一天起始时间和最后一天结束时间. package com.fline.aic.utils; import java.text.DateFormat ...
- SQL反模式学习笔记18 减少SQL查询数据,避免使用一条SQL语句解决复杂问题
目标:减少SQL查询数据,避免使用一条SQL语句解决复杂问题 反模式:视图使用一步操作,单个SQL语句解决复杂问题 使用一个查询来获得所有结果的最常见后果就是产生了一个笛卡尔积.导致查询性能降低. 如 ...
- ionic2中使用videogular2实现m3u8文件播放
// 安装依赖 npm i videogular2 --save npm i hls.js --save // 在index.html中引入 <script src="assets/h ...
- [Error]Python虚拟环境报错 OSError: setuptools pip wheel failed with error code 2
mkvirtualenv py35 python新建虚拟环境报错,setuptools pip wheel failed with error code 2 刚好昨天在CentOS安装的时候也总是报s ...
- python统计字词练习
方法一: import operator from nltk.corpus import stopwords stop_words = stopwords.words('English')#目的是去除 ...
- MyBatis入门(一)SqlSessionFactory
(1)新建数据库 CREATE TABLE emp ( id INT ( 10 ) PRIMARY KEY auto_increment, last_name VARCHAR(255), gender ...
- TypeScript -访问修饰符
class test extends egret.DisplayObjectContainer { public constructor() { /** * 1.不添加构造函数constructor ...
- node版本升级参考
https://www.cnblogs.com/sese/p/9557161.html