Print values from list based from separate text file
<p>How do I print a list of words from a separate text file? I want to print all the words unless the word has a length of 4 characters.  </p>

<p>words.txt file looks like this:</p>

<p>abate  chicanery   disseminate gainsay latent  aberrant    coagulate   dissolution garrulous   laud</p>

<p>It has 334 total words in it. I'm trying to display the list until it reaches a word with a length of 4 and stops. </p>

<pre><code>wordsFile = open("words.txt", 'r')
words = wordsFile.read()
wordsFile.close()
wordList = words.split()

#List outputs length of words in list

lengths= [len(i) for i in wordList]
for i in range(10):
    if i &gt;= len(lengths):
         break
    print(lengths[i], end = ' ')

# While loop displays names based on length of words in list

while words != 4:
    if words in wordList:
        print("\nSelected words are:", words)
    break
</code></pre>

<h1>output</h1>

<pre><code>5 9 11 7 6 8 9 11 9 4
</code></pre>

<h1>sample desired output</h1>

<p>Selected words are:</p>

<p>Abate </p>

<p>Chicanery </p>

<p>disseminate </p>

<p>gainsay </p>

<p>latent  </p>

<p>aberrant    </p>

<p>coagulate   </p>

<p>dissolution </p>

<p>garrulous   </p>

<p>To read all words from a text file, and print each of them unless they have a length of 4:</p>

<pre><code>with open("words.txt","r") as wordsFile:
  words = wordsFile.read()
  wordsList = words.split()
  print ("Selected words are:")
  for word in wordsList:
    if len(word) != 4:     # ..unless it has a length of 4
      print (word)
</code></pre>

<p>Later in your question you write, "I'm trying to display the first 10 words "...).  If so, add a counter, and add a condition to print if its value is &lt;= 10.</p>

python python-3.x