title: Python searching for exact word/phrase within a text file
body: <p>Currently, I'm trying to search for an exact word/phrase in a text file. I am using Python 3.4</p>
  
  <p>Here is the code I have so far.</p>
  
  <pre><code>import re
  
  def main():
      fileName = input("Please input the file name").lower()
      term = input("Please enter the search term").lower()
  
      fileName = fileName + ".txt"
  
      regex_search(fileName, term)
  
  def regex_search(file,term):
      source = open(file, 'r')
      destination = open("new.txt", 'w')
      lines = []
      for line in source:
          if re.search(term, line):
              lines.append(line)
  
      for line in lines:
          destination.write(line)
      source.close()
      destination.close()
  '''
  def search(file, term): #This function doesn't work
      source = open(file, 'r')
      destination = open("new.txt", 'w')
      lines = [line for line in source if term in line.split()]
  
      for line in lines:
          destination.write(line)
      source.close()
      destination.close()'''
  main()
  </code></pre>
  
  <p>In my function regex_search I use regex to search for the particular string. However, I don't know how to search for a particular phrase.</p>
  
  <p>In the second function, search, I split the line into a list and search for the word in there. However, this won't be able to search for a particular phrase because I am searching for ["dog walked"] in ['the','dog','walked'] which won't return the correct lines.</p>