title: How do I get the specific number of a word in a txt file?
body: <p>I'm trying to find whenever one of some specific words is used in a TXT file and then count what number word in the file the word is. My code returns the number for some but not all of the words, and I have no idea why. </p>
  
  <p>My code right now goes through the file word by word with a counter and returns the number if the word matches one of the words I want. </p>
  
  <pre class="lang-py prettyprint-override"><code>def wordnumber(file, filewrite, word1, word2, word3):
      import os
      wordlist = [word1, word2, word3]
      infile = open(file, 'r')
      g = open(filewrite, 'w')
      g.write("start")
      g.write(os.linesep)
      lines = infile.read().splitlines()
      infile.close()
      wordsString = ' '.join(lines)
      words = wordsString.split()
      n = 1
      for w in words:
          if w in wordlist:
              g.write(str(n))
              g.write(os.linesep)
          n = n+1
  </code></pre>
  
  <p>This works sometimes, but for some text files it only returns some of the numbers and leaves others blank.</p>