Introduction
Counting the number of words in a file is a common task when processing text data, such as analyzing documents, logs, or any textual content. This Python program reads a file, counts the number of words, and displays the result. The program handles text by reading each line, splitting it into words, and counting the total number of words.
Example:
- File Content (
example.txt):Hello, World! Welcome to Python file handling. Counting words is easy with Python. - Program Output:
Number of words in the file: 10
Problem Statement
Create a Python program that:
- Opens a file for reading.
- Reads the content of the file.
- Counts the number of words in the file.
- Displays the total word count.
Solution Steps
- Specify the File Name: Provide the name of the file to be read.
- Open the File: Use the
open()function to open the file in read mode. - Read the File Content: Use a loop to read each line of the file.
- Split Each Line into Words: Use the
split()method to break each line into words. - Count the Words: Sum the number of words for each line to get the total word count.
- Display the Word Count: Print the total number of words in the file.
- Close the File: Ensure the file is properly closed after reading.
Python Program
# Python Program to Count the Number of Words in a File
# Author: https://www.rameshfadatare.com/
# Step 1: Specify the file name
file_name = "example.txt"
# Step 2: Initialize a word count variable
word_count = 0
# Step 3: Open the file in read mode
try:
with open(file_name, "r") as file:
# Step 4: Read the file line by line
for line in file:
# Step 5: Split each line into words
words = line.split()
# Step 6: Count the words in the line
word_count += len(words)
# Step 7: Display the total word count
print(f"Number of words in the file: {word_count}")
except FileNotFoundError:
print(f"The file '{file_name}' does not exist.")
except IOError:
print(f"An error occurred while reading the file '{file_name}'.")
Explanation
Step 1: Specify the File Name
- The variable
file_nameis assigned the name of the file to be read. Ensure the file exists in the same directory as the Python script, or provide the full path to the file.
Step 2: Initialize a Word Count Variable
- The
word_countvariable is initialized to zero. This variable will be used to accumulate the total number of words.
Step 3: Open the File in Read Mode
- The
open()function is used to open the file in read mode ("r"). Thewithstatement ensures that the file is automatically closed after reading, even if an error occurs.
Step 4: Read the File Line by Line
- A
forloop is used to iterate over each line in the file.
Step 5: Split Each Line into Words
- The
split()method is used to break each line into a list of words. The default behavior ofsplit()is to split by any whitespace and remove it.
Step 6: Count the Words in the Line
- The
len()function is used to count the number of words in the list, and this count is added toword_count.
Step 7: Display the Total Word Count
- The
print()function is used to display the total number of words in the file.
Step 8: Handle Exceptions
- The program includes exception handling using
try-exceptblocks to catch and handle errors such asFileNotFoundErrorif the file does not exist, orIOErrorfor general input/output errors.
Output Example
Example Output:
Number of words in the file: 10
(Assuming example.txt contains the text provided in the introduction.)
Additional Examples
Example 1: Counting Words in a Large File
# Counting words in a large file
file_name = "large_file.txt"
word_count = 0
try:
with open(file_name, "r") as file:
for line in file:
words = line.split()
word_count += len(words)
print(f"Number of words in the large file: {word_count}")
except FileNotFoundError:
print(f"The file '{file_name}' does not exist.")
except IOError:
print(f"An error occurred while reading the file '{file_name}'.")
Output:
- The program reads each line of
large_file.txt, counts the words, and displays the total count.
Example 2: Counting Words in a File with Special Characters
# Counting words in a file with special characters
file_name = "special_chars.txt"
word_count = 0
try:
with open(file_name, "r") as file:
for line in file:
words = line.split()
word_count += len(words)
print(f"Number of words in the file with special characters: {word_count}")
except FileNotFoundError:
print(f"The file '{file_name}' does not exist.")
except IOError:
print(f"An error occurred while reading the file '{file_name}'.")
Output:
- The program counts words even if the file contains special characters, as
split()handles splitting by whitespace.
Example 3: Counting Words in a File Line by Line and Displaying Line Numbers
# Counting words in a file line by line and displaying line numbers
file_name = "example.txt"
word_count = 0
try:
with open(file_name, "r") as file:
line_number = 0
for line in file:
line_number += 1
words = line.split()
num_words = len(words)
word_count += num_words
print(f"Line {line_number}: {num_words} words")
print(f"\nTotal number of words in the file: {word_count}")
except FileNotFoundError:
print(f"The file '{file_name}' does not exist.")
except IOError:
print(f"An error occurred while reading the file '{file_name}'.")
Output:
Line 1: 2 words
Line 2: 4 words
Line 3: 4 words
Total number of words in the file: 10
Conclusion
This Python program demonstrates how to count the number of words in a file by reading the file line by line and using the split() method to separate words. The program includes exception handling to manage errors that may occur during file operations, making it robust for real-world applications. Understanding how to count words in a file is essential for tasks such as text analysis, document processing, or simple data analysis in Python.