Count the number of times a word appears in a text file python – Python Program to Count the Number of Words in a Text File

Count the number of times a word appears in a text file python: Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

This tutorial will show you how to use Python to count the number of words in a file. One Python program will be written to count the total number of words in a text file. The path to the file will be passed to the application as an input. This tutorial will teach you how to open a file and read its contents in Python. You’ll also learn how to get a list of all the words in a string.

Python provides a wide range of useful techniques for working with files. We don’t require any additional modules to work on any file. Using these methods, you may quickly read from a file, write to a file, or even append any content to a file.

Program to Count the Number of Words in a Text File

1)Algorithm

  • Make a single variable to hold the file path. This is a fixed variable. In the example below, you must replace this value with the file path from your own system. In addition, one more variable should be set to hold the overall number of words. Set the value of this variable to zero.
  • In read-only mode, open the file. In this example, we are merely reading the file’s content. Read mode will enough for counting the number of words in the file.
  • Using a loop, iterate through each line of the file. We can iterate through the lines one by one because this is a text file.
  • Split the line into words within the loop. Determine the total number of words and add them to the variable that holds the total number of words. Add the count of each line to this variable on each loop iteration.
  • When the loop is finished, the word count variable will have the total number of words in the text file. This variable’s value will be printed to the user.

2)Implementation

Below is the implementation:

# taking a variable which stores count and initializing it to 0
word_count = 0
# given file name
given_filename = "wordsfile.txt"
# opening the file in reading mode
with open(given_filename, 'r') as givenfile:
    # traversing the lines of file using for loop
    for fileline in givenfile:
      # split the line into words using split() function.
      # increasing the word count by counting the number of words in the file
        word_count += len(fileline.split())

print("The total number of files present in the given file = ", word_count)

Output:

The total number of files present in the given file = 103

3)Explanation:

  • The steps outlined in the algorithm above are used to implement the program. The variable ‘word count’ is used to store the total number of words in the text file. This variable’s value is set to zero at the start. If a word is found, we shall add one to this variable.
  • The variable ‘file name’ is used to store the file path. Change the value of this variable to your own file location.
  • You can find the path of a file by dragging and dropping it onto the terminal. If you do not modify this variable value, the program will not run.
  • We’re going to open the file in reading mode. To open a file, use the open() method. The method’s first parameter is the file’s path, and its second parameter is the mode for opening the file. We’re using the character ‘r’ to indicate read-mode when we open the file.
  • We are iterating through the lines of the file using a single ‘for loop.’
  • The split() method is used within the loop to separate the line. This method returns a single list containing the string’s words. The length of this list is the number of words in that line. The len() method is used to calculate the word count. This value is being added to the variable word count.
  • The word count variable holds the overall word count in the file at the end of the program. Display its value to the user.

Sample Implementation in google colab: