Write a function in Python to count the words this and these present in a text file article txt

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.

  1. Open the file in read mode and handle it in text mode.
  2. Read the text using read() function.
  3. Split the text using 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.

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 : 14

Example 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 : 21

Summary

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()


How to count the lines words and characters within a text file in Python?

Counting of lines, words and letters in a text file.
The counter of lines is incremented by 1..
The string is split into words using the split method. The len function counts the number of words in the list of words. ... .
To ignore the newline character ( '\n' ), we remove it using the strip method..

How to count how many times a word appears in a text file Python?

Many times it is required to count the occurrence of each word in a text file. To achieve so, we make use of a dictionary object that stores the word as the key and its count as the corresponding value. We iterate through each word in the file and add it to the dictionary with a count of 1.

How to word count in Python?

Count words in a sentence in Python program.
Approach 1 − Using split() function. Split function breaks the string into a list iterable with space as a delimiter. ... .
Approach 2 − Using regex module. ... .
Approach 3 − Using sum()+ strip()+ split() function..