In this program, we need to count the words present in given text file. This can be done by opening a file in read mode using file pointer. Read the file line by line. Split a line at a time and is stored in an array. Iterate through the array and count the word.
The content of data.txt file used in the program is shown below.
A computer program is a collection of instructions that performs specific task when executed by a computer.
Computer requires programs to function.
Computer program is usually written by a computer programmer in programming language.
A collection of computer programs, libraries, and related data are referred to as software.
Computer programs may be categorized along functional lines, such as application software and system software.
You can count 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 number of words in 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 read() function.
- Split the text using 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.
Example 1: Count Number of Words
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))Output
Number of words in text file : 14Example 2: Count Number of Words in 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.
New line character separates lines in a text file. New line is a white space character and when we split the whole data in text file using split() method, all the words in all the sentences are split and returned as a single list.
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))Output
Number of words in text file : 21Summary
In this tutorial of Python Examples, we learned how to count number of words in a Text File, with the help of example programs.
Write a function in Python to count the words "this" and "these" present in a text file "article.txt". [Note that the words "this" and "these" are complete words]
Source Code
def count_words(): file = open("article.txt","r") count = 0 data = file.read() words = data.split() for word in words: if word == 'this' or word =='these': count+=1 print(count) file.close() count_words()