打印输出附件文件的有效行数,注意:空行不计算为有效行数。
分析:文件中的空行只包含’\n’, 有空格的行,算有效行。因此用strip 去掉’\n’,如果其余的字符长度为0则为无效行,不统计ct。否则 ct 加一(Python中非0 为True 0为False)
fo = open("latex.log")
ct = 0
for line in fo.readlines():
if len(line.strip('\n')):
ct += 1
print("共{}行".format(ct))