This little script delivers the number of words in a text-file.
Our example uses the poem “The Raven” by Edgar Allen Poe
but the program works for any .txt-File.
#! Count words in a .txt file # open a given txt-file and read it line by line # split the words from the string with line.split() import os num_words = 0 with open("the_raven.txt", 'r') as f: for line in f: words = line.split() num_words += len(words) print("Number of words:") # Output: Number of words: print(num_words) # Output: 1067