Python EOF识别是否有一行

时间:2013-07-26 15:04:14

标签: python python-2.7

我是python中的新手,这是我的代码

for myPath,item in dicts.items():
   for elem in item:
     thefile = open(elem, 'r')
     for line1,line2 in itertools.izip_longest(*[thefile]*2):
        if ('namespace' in line1) or ('namespace' in line2):
           return True
        if line2 is None:  (this condition dont work)
           continue

我需要确定line2是否在EOF之后才能获取项

中的另一个元素

现在我的情况不行。 感谢

1 个答案:

答案 0 :(得分:1)

如果您只是想避免TypeError中的'namespace' in None,那么请改为采用另一种方式,让line2为空字符串。请注意,我已经添加iter,因为我假设您正在尝试将文件分组成对...(但后来不确定这会逐行增加...)

for line1,line2 in itertools.izip_longest(*[iter(thefile)]*2, fillvalue='')

注意:

您的整个标准看起来可能是(感谢善意发现错误):

return any('namespace' in line for line in thefile)