Python Program to Count the Number of Words in a File

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

  1. Specify the File Name: Provide the name of the file to be read.
  2. Open the File: Use the open() function to open the file in read mode.
  3. Read the File Content: Use a loop to read each line of the file.
  4. Split Each Line into Words: Use the split() method to break each line into words.
  5. Count the Words: Sum the number of words for each line to get the total word count.
  6. Display the Word Count: Print the total number of words in the file.
  7. 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_name is 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_count variable 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"). The with statement ensures that the file is automatically closed after reading, even if an error occurs.

Step 4: Read the File Line by Line

  • A for loop 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 of split() 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 to word_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-except blocks to catch and handle errors such as FileNotFoundError if the file does not exist, or IOError for 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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top