I want to check if a string is in a text file. If it is, do X. If it\'s not, do Y. However, this code always returns True
for some reason. Can anyone see what i
Two problems:
Your function does not return anything; a function that does not explicitly return anything returns None (which is falsy)
True is always True - you are not checking the result of your function
.
def check(fname, txt):
with open(fname) as dataf:
return any(txt in line for line in dataf)
if check('example.txt', 'blabla'):
print "true"
else:
print "false"