Program to Count the Occurrences of a Word in a Text File

Python Program to Count the Occurrences of a Word in a Text File

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

Files and File Handling in Python:

A file is a piece of information or data that is saved in computer storage devices. One of the most crucial aspects of any language is file handling. The Python programming language allows two types of files. The first is a text file, which stores data in the form of text that humans and computers can read. The second type is a binary file, which stores binary data and is only readable by a computer. I

File handling is the process of managing files on a file system. Each operating system has its own method of storing files.

Python file handling comes in useful when working with files in our projects. We don’t need to be concerned about the underlying operating system or its file system rules and operations.

Program to Count the Occurrences of a Word in a Text File in Python

Below is the full approach to calculate the number of occurrences of the given word in the given text file in python

Approach:

  • Take a variable to say wordCount that stores the count of the given word in a given file and initializes it to 0.
  • Scan the given word by user input using the int(input()) function and store it in a variable.
  • Create the file or upload the existing file.
  • Enter the file’s name into the input() function and save it to a variable.
  • Open the file with the entered file name in read mode.
  • Traverse the lines in the file using a For loop.
  • Using the split() method, split the line into words.
  • To traverse the words in the list, use a For loop.
  • If the iterator value is equal to the given word then increase the wordCount by 1.
  • Print the wordCount of the given word.
  • Exit of Program

Below is the implementation:

# Take a variable say wordCount that stores the calculate the
# count of given word in a given file and initialize it to 0.
wordCount = 0
# Scan the given word by user input using the int(input()) function and store it in a variable.
givenword = input('Enter the given word = ')
# Enter the file's name into the input() function and save it to a variable.
filename = input("Enter the file name = ")
# Open the file with the entered file name in read mode.
with open(filename, 'r') as givenfile:
  # Traverse the lines in the file using a For loop.
    for fileline in givenfile:
        # Using the split() method, split the line into words.
        wordslist = fileline.split()
        # To traverse the words in the list, use a For loop.
        for words in wordslist:
            # If the iterator value is equal to the given word then increase the wordCount by 1.
            if(words == givenword):
                wordCount = wordCount+1
# Print the wordCount.
print('The total count of the given word {',
      givenword, '} in the given file = ', wordCount)

Output:

Enter the given word = btechgeeks Enter the file name = hello.txt 
The total count of the given word { btechgeeks } in the given file = 3

Explanation:

  • A file name and the word to be searched must be entered by the user.
  • In read mode, the file is opened with the open() method.
  • To read through each line of the file, a for loop is utilized.
  • Using split(), each line is divided into a list of words.
  • Another for loop is used to scan the list, and each word in the list is compared to the word given by the user.
  • If both words are the same, the word count is increased.
  • The total number of instances of the word is displayed.
  • Exit of the program.

hello.txt

hello this is btechgeeks python programming learning platform btechgeeks btechgeeks btechgeeks.

Google Colab Images:

Files and Code:

Code:

Output Image:

Hello.txt


Related Programs: