Count words in a text file

Run the word counter from the project directory to print a single total. It reads UTF-8 text and counts runs of characters separated by whitespace.

Run the counter

With Python 3 installed, save a text file called draft.txt beside wordcount.py. Try this one-line draft:

Keep the first paragraph short.

Pass its path to the script:

python3 wordcount.py draft.txt

The example prints 5. A blank file prints 0.

How punctuation is counted

The script splits text wherever it finds whitespace. A hyphenated phrase like well-written counts as one word. A comma touching a word stays part of that same word.