title: Read text file and look for certain words from key word list
body: <p>I am new to Python, and I am trying to build a script where I import text_file_1 that contains a body of text. I want the script to read the body of text, and look for certain words that I have defined in a list called (key_words) that contain words with a Capital letter in the beginning (Nation) and lowercase (nation). After Python does the searching, it will output the list of words vertically in a new text file called "List of Words", along with the number of times that word occurs in the body. If I read text_file_2 with a body of text, it will do the same, but ADD to the List of Words from the original file.</p>
  
  <p>Example:</p>
  
  <p>List of Words</p>
  
  <p>File 1:</p>
  
  <pre><code>God: 5
  Nation: 4
  creater: 8
  USA: 3 
  </code></pre>
  
  <p>File 2:</p>
  
  <pre><code>God: 10
  Nation: 14
  creater: 2
  USA: 1
  </code></pre>
  
  <p>Here is what I have so far:</p>
  
  <pre><code>from sys import argv
  from string import punctuation
  
  script = argv[0] all_filenames = argv[1:]
  
  print "Text file to import and read: " + all_filenames
  print "\nReading file...\n"
  text_file = open(all_filenames, 'r')
  all_lines = text_file.readlines()
  #print all_lines
  text_file.close()
  
  for all_filenames in argv[1:]:
     print "I get: " + all_filenames
  
  print "\nFile read finished!"
  #print "\nYour file contains the following text information:"
  #print "\n" + text_file.read()
  
  #~ for word, count in word_freq.items():
      #~ print word, count
  
  keyWords = ['God', 'Nation', 'nation', 'USA', 'Creater', 'creater', 'Country', 'Almighty',
               'country', 'People', 'people', 'Liberty', 'liberty', 'America', 'Independence', 
               'honor', 'brave', 'Freedom', 'freedom', 'Courage', 'courage', 'Proclamation',
               'proclamation', 'United States', 'Emancipation', 'emancipation', 'Constitution',
               'constitution', 'Government', 'Citizens', 'citizens']
  
  for word in keyWords:
      if word in word_freq:
          output_file.write( "%s: %d\n" % (word, word_freq[word]) )
  
  output_file = open("List_of_words.txt", "w")
  
  for word in keyWords:
      if word in word_freq:
          output_file.write( "%s: %d\n" % (word, word_freq[word]) )
  
  output_file.close()
  </code></pre>
  
  <p>Maybe use this code somehow?</p>
  
  <pre><code>import fileinput
  for line in fileinput.input('List_of_words.txt', inplace = True):
      if line.startswith('Existing file that was read'):
          #if line starts Existing file that was read then do something here
          print "Existing file that was read"
      elif line.startswith('New file that was read'):
          #if line starts with New file that was read then do something here
          print "New file that was read"
      else:
          print line.strip()
  </code></pre>