title: In Python 3, how can I search for a string in a separate .txt file?
body: <p>I am trying to write a code in Python 3 that will allow the user to enter the name of a .txt file. Then the user enters a keyword or phrase and Python searches that .txt file and returns what line the phrase is on. Also, the program will write some things to a .html file, but I can take care of that once I get this part figured out. Here is my code so far:</p>
  
  <pre><code>def main():            # Main function
  
      list_variable = 0
      search_term = 0
      line = 0
      links = 0       #Set list variables search and links equal to empty
      print("The Great Search Engine, by Jackson Reeves")#Print Intro
      filename = input("Enter name of database file: ")#Ask for filename
      infile = open(filename, "r")                #Use read function to read file
      keyword = filename.find(input("Enter keyword to search for: "))
      for line in filename.readlines():
          line = line + 1
      url = "http://www.aol.com"
      url2 = "http://www.toyota.com"
      url3 = "http://www.uky.edu"
      url4 = "http://www.google.com"
      url5 = "http://www.eku.edu"
      url6 = "http://www.yahoo.com"
      url7 = "http://www.youtube.com"
      url8 = "http://www.msn.com"
  
  
  #if keyword exists in file, execute these statements
      p1 = ("&lt;html&gt;\n")
      out_file = open("mypage.html", "wt")
      out_file.write(p1)
  
      p2 = ("&lt;head&gt;&lt;title&gt;Search Findings&lt;/title&gt;&lt;/head&gt;\n")
      out_file.write(p2)
  
      p3 = ("&lt;body&gt;\n")
      out_file.write(p3)
  
      p4 = ('&lt;h2&gt;&lt;p align=center&gt;Search for "'+keyword+'"&lt;/h2&gt;\n')
      out_file.write(p4)
  
      p5 = ("&lt;p align=center&gt;\n")
      out_file.write(p5)
  
      p6 = ("&lt;table border&gt;\n")
      out_file.write(p6)
  
      p7 = ("&lt;tr&gt;&lt;th&gt;Hit&lt;th&gt;URL&lt;/tr&gt;\n")
      out_file.write(p7)
  
      #if keyword is in line one, add this statement
      p8 = ('&lt;tr&gt;&lt;td&gt;search &lt;b&gt;"'+keyword+'"&lt;/b&gt;&lt;td&gt;&lt;a href="'+url+'"&gt; '+url+'&lt;/a&gt;&lt;/tr&gt;\n')
      out_file.write(p8)
  
      #if keyword is in line three, add this statement
      p9 = ('&lt;tr&gt;&lt;td&gt;search &lt;b&gt;"'+keyword+'"&lt;/b&gt;&lt;td&gt;&lt;a href="'+url2+'"&gt; '+url2+'&lt;/a&gt;&lt;/tr&gt;\n')
      out_file.write(p9)
  
      #if keyword is in line five, add this statement
      p10 = ('&lt;tr&gt;&lt;td&gt;search &lt;b&gt;"'+keyword+'"&lt;/b&gt;&lt;td&gt;&lt;a href="'+url3+'"&gt; '+url3+'&lt;/a&gt;&lt;/tr&gt;\n')
      out_file.write(p10)
  
      #if keyword is in line seven, add this statement
      p11 = ('&lt;tr&gt;&lt;td&gt;search &lt;b&gt;"'+keyword+'"&lt;/b&gt;&lt;td&gt;&lt;a href="'+url4+'"&gt; '+url4+'&lt;/a&gt;&lt;/tr&gt;\n')
      out_file.write(p11)
  
      #if keyword is in line nine, add this statement
      p12 = ('&lt;tr&gt;&lt;td&gt;search &lt;b&gt;"'+keyword+'"&lt;/b&gt;&lt;td&gt;&lt;a href="'+url5+'"&gt; '+url5+'&lt;/a&gt;&lt;/tr&gt;\n')
      out_file.write(p12)
  
      #if keyword is in line eleven, add this statement
      p13 = ('&lt;tr&gt;&lt;td&gt;search &lt;b&gt;"'+keyword+'"&lt;/b&gt;&lt;td&gt;&lt;a href="'+url6+'"&gt; '+url6+'&lt;/a&gt;&lt;/tr&gt;\n')
      out_file.write(p13)
  
      #if keyword is in line thirteen, add this statement
      p14 = ('&lt;tr&gt;&lt;td&gt;search &lt;b&gt;"'+keyword+'"&lt;/b&gt;&lt;td&gt;&lt;a href="'+url7+'"&gt; '+url7+'&lt;/a&gt;&lt;/tr&gt;\n')
      out_file.write(p14)
  
      #if keyword is in line thirteen, add this statement
      p15 = ('&lt;tr&gt;&lt;td&gt;search &lt;b&gt;"'+keyword+'"&lt;/b&gt;&lt;td&gt;&lt;a href="'+url8+'"&gt; '+url8+'&lt;/a&gt;&lt;/tr&gt;\n')
      out_file.write(p15)
  
      p16 = ("&lt;/table&gt;\n")
      out_file.write(p16)
  
      p17 = ("&lt;/body&gt;\n")
      out_file.write(p17)
  
      p18 = ("&lt;/html&gt;\n")
      out_file.write(p18)
  
      out_file.close
  
  #if no keyword exists in file, execute these statements
      p19 = ("&lt;html&gt;\n")
      out_file = open("mypage.html", "wt")
      out_file.write(p19)
  
      p20 = ("&lt;head&gt;&lt;title&gt;Search Findings&lt;/title&gt;&lt;/head&gt;\n")
      out_file.write(p20)
  
      p21 = ("&lt;body&gt;\n")
      out_file.write(p21)
  
      p22 = ('&lt;h2&gt;&lt;p align=center&gt;Search for "'+keyword+'"&lt;/h2&gt;\n')
      out_file.write(p22)
  
      p23 = ("&lt;p align=center&gt;\n")
      out_file.write(p23)
  
      p24 = ("&lt;table border&gt;\n")
      out_file.write(p24)
  
      p25 = ('&lt;tr&gt;&lt;td&gt;'+keyword+' &lt;td&gt; not found &lt;/tr&gt;\n')
      out_file.write(p25)
  
      p26 = ('&lt;/table&gt;\n')
      out_file.write(p26)
  
      p27 = ('&lt;/body&gt;\n')
      out_file.write(p27)
  
      p28 = ('&lt;/html&gt;\n')
      out_file.write(p28)
  
      out_file.close
  
  
  
  main()
  </code></pre>