统计python代码行数
发表于:2024-10-16 作者:千家信息网编辑
千家信息网最后更新 2024年10月16日,addhosts项目已接近尾声,我很想知道我们写了多少行代码。一、需求统计源码目录下py文件的代码行数。二、脚本分析获取指定目录下所有的.py文件,对文件进行遍历;读取每个文件,对文件内容进行遍历,过
千家信息网最后更新 2024年10月16日统计python代码行数
addhosts项目已接近尾声,我很想知道我们写了多少行代码。
一、需求
统计源码目录下py文件的代码行数。
二、脚本分析
获取指定目录下所有的.py文件,对文件进行遍历;
读取每个文件,对文件内容进行遍历,过滤掉空行和注释;
三、实现及结果
#coding:utf-8import osclass StatLines(object): def __init__(self,path): self.path = path def stat_lines(self): file_list = os.listdir(self.path) os.chdir(self.path) total = 0 for file in file_list: if file.endswith('.py'): lines = open(file, encoding='utf-8').readlines() count = 0 for line in lines: if line == '\n': continue elif line.startswith('#'): continue else: count += 1 total += count print('%s has %d lines' %(file,count)) print('total lines is: %d' %total)if __name__ == '__main__': sl = StatLines('E:\\Python_Project\\addhost_v2\\addhosts') sl.stat_lines()
运行结果如下:
四、总结
问题:
在执行open(file).readlines()时,遇到了如下错误
"UnicodeDecodeError: 'gbk' codec can't decode byte 0xaf in position 548: illegal multibyte sequence"
解决方法:
在open时,设置encoding='utf-8'后,问题得到解决。
文件
代码
目录
结果
问题
统计
内容
尾声
方法
注释
源码
空行
脚本
错误
需求
项目
分析
运行
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
华为云服务器加内存
阴阳师如何转换服务器
网吧服务器集中管理
威海荣成乳山文登软件开发
服务器访问被拒绝访问
学计算机网络技术有那些用
网络安全平台排名
软件开发 amd
网络安全青少年怎么做图片
从软件开发到当上老板
思科网络技术学院教程考试
word调取数据库
闵行区环保网络技术解决方案
翰高 数据库
无法激活服务器暂时不可用请尝试
山西省公安厅网络安全管理
济宁急需软件开发
长沙网络安全员考试
vagaa服务器节点列表
火鸟数据库导出excel
yii 写入数据库
金华轶诺网络技术有限公司
蚌埠软件开发多少钱
数据库理论基础王珊
网络安全深圳招聘
联想服务器怎样换内存
postfix服务器
网络安全法共有
saas系统数据库分离
北京市鑫丹妮网络技术公司照片