user2623211
user2623211

Reputation: 1

Python EOF identify if a line is after

I'm new in python, this is my code

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

I need to identify if line2 is after EOF to take another element in item

now my condition dont work. Thanks

Upvotes: 0

Views: 54

Answers (1)

Jon Clements
Jon Clements

Reputation: 142166

If you're just trying to avoid the TypeError from 'namespace' in None, then do it another way instead and have line2 be an empty string. Note that iter has been added as I assume you're trying to group the file into pairs... (but then not sure what this gains over just line by line...)

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

Note:

Your entire criteria looks like it could be (thanks to kindall for spotting mistake):

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

Upvotes: 1

Related Questions