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.
- Open the file in read mode and handle it in text mode.
- Read the text using the
read()
function. - Split the text using a space separator. We assume that words in a sentence are separated by a space character.
- The length of the split list should equal the number of words in the text file.
- 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):
- Use
open()
to open the filedata.txt
in read mode ("rt"
stands for text mode). - Read the entire content of the file into a variable
data
using theread()
method. - Use
split()
to break the text into words based on spaces and store them in a list namedwords
. - Count the number of elements in the list
words
usinglen(words)
. - 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):
- Open the file in read mode using
open()
. - Read all lines of the file into the variable
data
. - Use
split()
, which splits all words from all lines into a single list, as newline characters are treated as whitespace. - Count the words using
len()
on the resulting list. - 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):
- Open the file in read mode and store its content in
data
. - Use the
re.sub()
function to remove special characters ([^a-zA-Z0-9\s]
matches anything that is not a letter, digit, or whitespace). - Split the cleaned data into words using
split()
. - Count the number of words using
len()
on the cleaned list. - 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.