【发布时间】:2014-09-27 03:07:10
【问题描述】:
我正在尝试逐行读取文件并做一些事情。问题是,如果我在文件末尾添加一堆空行,我会得到一个异常(列表索引超出范围)。
def check_ip(address):
try:
socket.inet_aton(address)
return True
except:
return False
def myfunction:
with open(filename, 'r') as f:
for line in f.readlines():
if not line: continue
tokens = line.strip().split()
if not check_ip( tokens[0] ): continue
// do some stuff
【问题讨论】: