-4

I want to know how I can search for a specific word in a .txt file using Python3. If the word 'pizza' is included in the .txt file, then it should (e.g.) import another python program.

Sam D
  • 23
  • 5
RedDebian
  • 1
  • 3

1 Answers1

0

You can iterate through each line in the file with a 'for' loop and check to see if the string is in the line:

f = open('file.txt', 'r')
for line in f:
   if 'pizza' in line:
      # do whatever you want here
      print("Found it!")
   else: 
      pass
Sam D
  • 23
  • 5