Python EOF识别一行是否在后面



我是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

我需要确定第2行是否在EOF之后,以获取项目中的另一个元素

现在我的状况不好了。感谢

如果您只是想从'namespace' in None中避免TypeError,那么请改用另一种方法,并将line2设为空字符串。请注意,添加了iter,因为我假设您正在尝试将文件分组成对。。。(但不确定这会在逐行扫描中获得什么…)

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

注:

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

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

相关内容

最新更新