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

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)



猜你喜欢

转载自blog.csdn.net/shifanfashi/article/details/89420656