Day 19: Count the Raven how many words?

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

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.