Home > Mobile >  Return False if specific string is not in a text file
Return False if specific string is not in a text file

Time:11-22

I have this function with looks for a specific string in a text file:

def check_for_string(string, file):
   with open(file) as target_file:
      for index, line in enumerate(target_file):
         if string in line:
            return True
            break

And it stops when it finds the specific string and returns True. How can I do it, so it returns True when it found the string and returns False if it did not found the string?

CodePudding user response:

Just return False:

def check_for_string(string, file):
   with open(file) as target_file:
      for index, line in enumerate(target_file):
         if string in line:
            return True
    return False

Note: You don't need to put break after return, it's useless.

  • Related