Eskimo868
Eskimo868

Reputation: 240

Return False if specific string is not in a text file

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?

Upvotes: 0

Views: 503

Answers (1)

ichramm
ichramm

Reputation: 6632

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.

Upvotes: 2

Related Questions