您的位置:首页 > 编程语言

第 0007 题: 有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。

2019-04-20 17:31 92 查看
import os
# os 模块用来处理文件夹及文件目录
import string
import re

num_code = 0
num_empty = 0
num_note = 0

os.chdir('E:\python\wenjian\ex-2')
# os.chdir 用来改变当前处理路径

f = open('spider_maoyan.py')
read_f = f.readlines()
f.close()
# 读取文件中的代码(此时包含了所有)

pattern = '.*#'
# 匹配正则表达式

for i in read_f:
if  "#" in i:
if re.findall(pattern,i)[0][:-1].isspace() or re.findall(pattern, i)[0][:-1] == "":
num_note += 1
else:
num_code += 1
elif i.isspace():
# isspace() 方法检测字符串是否只由空格组成。
num_empty += 1
else:
num_code += 1

print('the number of code is %d' %(num_code + num_empty + num_note))
print('the number of empty is %d' % num_empty)
print('the number of note is %d' % num_note)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐