练习1:文件行数
描述
打印输出附件文件的有效行数,注意:空行不计算为有效行数。
输入输出示例
这是仅给出输出格式样例,不是结果。
输入
输出
示例 1
共100行
我的代码
#week7_1.py
#文件的行数
f=open("latex.log")
lines=0
for line in f:
if len(line)==1: #过滤掉空行,空行只有一换行符
continue
lines +=1
print("共{}行".format(lines))
参考代码
f = open("latex.log")
s = 0
for line in f:
line = line.strip('\n') #将每一行字符串的首尾的换行符去掉
if len(line) == 0: #过滤掉空行
continue
s += 1
print("共{}行".format(s))
练习2ÿ