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
Your check
function should return the found
boolean and use that to determine what to print.
def check():
datafile = file('example.txt')
found = False
for line in datafile:
if blabla in line:
found = True
break
return found
found = check()
if found:
print "true"
else:
print "false"
the second block could also be condensed to:
if check():
print "true"
else:
print "false"