Count Number of Words in Text File - Python Examples


Python - Count Number of Words

You can count the number of words in a text file in Python by following a sequence of steps which we shall discuss in this tutorial.

In this tutorial, we shall learn how to count the number of words in a text file, using Python example programs.


Steps to Count Number of Words in Text File

To count the number of words in a text file, follow these steps.

  1. Open the file in read mode and handle it in text mode.
  2. Read the text using the read() function.
  3. Split the text using a space separator. We assume that words in a sentence are separated by a space character.
  4. The length of the split list should equal the number of words in the text file.
  5. You can refine the count by cleaning the string prior to splitting or validating the words after splitting.

Examples

1. Count Number of Words in a Given Text File

In this Python Example, we will read a text file and count the number of words in it. Consider the following text file:

Text File

Welcome to pythonexamples.org. Here, you will find python programs for all general use cases.

Python Program

file = open("C:\data.txt", "rt")
data = file.read()
words = data.split()

print('Number of words in text file :', len(words))

Explanation (Step-by-Step):

  1. Use open() to open the file data.txt in read mode ("rt" stands for text mode).
  2. Read the entire content of the file into a variable data using the read() method.
  3. Use split() to break the text into words based on spaces and store them in a list named words.
  4. Count the number of elements in the list words using len(words).
  5. Print the total number of words in the text file.

Output

Number of words in text file : 14

2. Count Number of Words in a Text File with Multiple Lines

In this Python Example, we will read a text file with multiple lines and count the number of words in it. Consider the following text file:

Text File - data.txt

Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
This is another line with some words.

Python Program

file = open("C:\data.txt", "rt")
data = file.read()
words = data.split()

print('Number of words in text file :', len(words))

Explanation (Step-by-Step):

  1. Open the file in read mode using open().
  2. Read all lines of the file into the variable data.
  3. Use split(), which splits all words from all lines into a single list, as newline characters are treated as whitespace.
  4. Count the words using len() on the resulting list.
  5. Print the total count.

Output

Number of words in text file : 21

3. Count Words in a Text File and Exclude Special Characters

In this example, we will clean the text to exclude special characters before counting the words.

Text File - data.txt

Welcome to pythonexamples.org! Here, you'll find: python programs, tips, and tricks.

Python Program

import re

file = open("C:\data.txt", "rt")
data = file.read()

# Remove special characters
cleaned_data = re.sub(r'[^a-zA-Z0-9\s]', '', data)
words = cleaned_data.split()

print('Number of words in text file :', len(words))

Explanation (Step-by-Step):

  1. Open the file in read mode and store its content in data.
  2. Use the re.sub() function to remove special characters ([^a-zA-Z0-9\s] matches anything that is not a letter, digit, or whitespace).
  3. Split the cleaned data into words using split().
  4. Count the number of words using len() on the cleaned list.
  5. Print the total word count.

Output

Number of words in text file : 13

Summary

In this tutorial of Python Examples, we learned how to count the number of words in a text file, handle multiple lines, and exclude special characters to refine the count. These examples illustrate how to use basic string operations and regular expressions for text processing.


Python Libraries