手机版
网站地图
美文美图
最新动态
你好,欢迎访问
好美文阅读网
个性皮肤
搜索
网站首页
美文
文章
散文
日记
诗歌
小说
故事
句子
作文
签名
祝福语
情书
范文
读后感
文学百科
美文摘抄
节日文章
名家散文
网名大全
座右铭
口号大全
面试技巧
说说大全
阅读答案
诗词默写
流言蜚语
节日祝福
好句子
经典台词
谚语大全
亲情故事
友情故事
表白情书
工作报告
活动总结
心得体会
专题汇总
美文网首页
实现python中的代码行数统计思路:
实现python中的代码行数统计思路:
作者:
叶_叶
| 来源:发表于
2018-03-16 14:18 被阅读0次
实现python中的代码行数统计思路:
统计文件中代码的总行数减去空行单行注释以及多行注释功能:
1.获取文件内容的总行数
2.排除空行 单行注释 多行注释
def code_statistics(path):
# # 打开这个文件
with open(path, 'r', encoding='utf-8') as openFile:
# 按列读取
fileline = openFile.readlines()
# 给非代码行一个变量
i = 0
# 整个文件里面内容的总行数 number_line = len(fileline)
# 给多行注释一个状态
note = False
# 遍历文件内容
for line in fileline:
# 空行
if line == '\n':
i += 1
# 单行注释
elif re.findall('[#]', line):
i += 1
# 多行注释开头 elif re.findall("\'\'\'", line) and note == False:
i += 1
note = True
# 多行注释结尾 elif re.findall("\'\'\'", line) and note == True:
i += 1
note = False
# 多行注释内部注释
elif note:
i += 1
num_code_line = number_line - i
print(num_code_line)
如果统计文件夹中的python文件的代码行数,首先就是要遍历文件目录,筛选出以.py结尾的文件,再去统计py文件里面的代码行数
def get_all_fire(path):
# 得到当前目录下的所有文件 file_list = os.listdir(path)
py_file_abs = []
# 遍历所有文件
for file_name in file_list:
# 获取文件及文件夹的绝对路径
file_abs = os.path.join(path, file_name)
if os.path.isfile(file_abs) and file_name.endswith('.py'): # 判断当前文件路径是否是文件和.py文件
py_file_abs.append(file_abs)
# 判断当前文件路径是不是文件夹
elif os.path.isdir(file_abs):
py_file_abs += get_all_fire(file_abs)
return py_file_abs
相关文章
网友评论
本文标题:
实现python中的代码行数统计思路:
本文链接:
https://www.haomeiwen.com/subject/uscufftx.html
延伸阅读
那年盛夏诗歌
环境监察队工作总结范文
优秀教师学习心得范文
华胥引的读后感300字
《Its red》教学反思范文
农资购销的合同范本
竞选中队委优秀演讲稿
辞金蹈海的成语解释
《世纪宝鼎》公开课教案设计
因为爱你,所以牵挂
今生今世红尘醉——美到
一个90后的内心独白
致已逝去的高中年华
深度阅读
您也可以注册成为美文阅读网的作者,发表您的原创作品、分享您的心情!
情人节
母亲节
重阳节
清明节
端午节
植树节
元宵节
妇女节
愚人节
圣诞节
父亲节
教师节
儿童节
劳动节
青年节
建军节
万圣节
平安夜
光棍节
中秋节
国庆节
感恩节
腊八节
更多话题
栏目导航
摄影
故事
互联网
读书
旅行
热点阅读
正能量日记35
原创《柔婉娇羞,岁月无忧》作者:徐淑英
周末漫步
【0308我在悦读】我的所有严厉,源自于我的焦虑
雨中的呼唤
你嘴上说爱我,却连家务都不愿做
你到底有没有牙周病看看这个就知道!
滚蛋吧,拖延症!
Alchemist's Lab
一个圆的世界02
网友评论