Python Count Words in File

In this section, we will learn about Python Count Words in File. Here we assume the file as a simple Text file (.txt). Also, we will cover these topics.

  • Python Count Words in File
  • Python Count Specific Words in File
  • Python Count Words in Multiple Files
  • Python Count Unique Words Files
  • Python Count Words in Excel File
  • Python Count Unique Words in Text File
  • Python Program to Count Number of Words in File
  • Python Count Word Frequency in a File
  • Python Word Count CSV File

Python Count Words in File

In this section, we will learn about python count words in file. In other words, we will learn to count the total number of words from a text file using Python.

  • The entire process is divided into three simple steps:
    • open a text file in read only mode
    • read the information of file
    • split the sentences into words and find the len.
  • Using file = open('file.txt', 'r') we can open the file in a read-only mode and store this information in a file variable.
  • read_data = file.read() this statement is used to read the entire data in one go and store it in a variable named read_data.
  • It’s time to split the sentences into words and that can be done using
    per_word = read_data.split() here split() method is used to split each sentence in read_data and all this information is stored in a variable named per_word.
  • final step is to print the length of per_word variable. Please note that lenght is counting total words in the file. Here is the statement to print a message with total count of words print('Total Words: ', len(per_word)).

Source Code:

Here is the source code to implement the Python Count Words in a File.

file = open('file.txt', 'r')
read_data = file.read()
per_word = read_data.split()

print('Total Words:', len(per_word))

Output:

Here is the output of counting words in a file using Python. In this output, the text file we have used has 221 words.

python count words in a file
Python Count Words in a File

Read: Python Counter

Python Count Specific Words in File

In this section, we will learn about Python Count Specific Words in File. The user will provide any word and our program will display the total occurrence of that word.

  • Occurrence of specific word can be counted in 5 simple steps:
    • Ask for user input
    • Open the file in read only mode
    • Read the data of the file
    • convert the data in lower case and count the occurrence of specific word
    • Print the count
  • seach_word_count = input('Enter the word') in this code, user input is collected and stored in a variable. Whatever word user will iput here that word will be searched in a file.
  • file = open('file.txt', 'r') in this code file.txt is a file that is opened in a read only mode and the result is stored in a ‘file’ variable.
  • Once we have opened a file, next step is to read the data in it so using the code read_data = file.read() we have read the entire data and stored the information in a variable named ‘read_data’.
  • word_count = read_data.lower().count(search_word_count) In this code, we have converted the data to lower case and using count method we have searched for the word that user has provided. The entire result is stored in a variable named ‘word_count’.
  • Last step in the process is to print the message with count. We have used formatted string to make our message descriptive. Here is the code for that.
    print(f"The word '{search_word_count}' appeard {word_count} times.")

Source Code

Here is the complete source code to perform python count specific words in a file.

# asking for user input
search_word_count = input('Enter the word: ')

# opening text file in read only mode
file = open("file.txt", "r")

# reading data of the file
read_data = file.read()

# converting data in lower case and the counting the occurrence 
word_count = read_data.lower().count(search_word_count)

# printing word and it's count
print(f"The word '{search_word_count}' appeared {word_count} times.")

Output:

Here is the output of Python Count Specific Word in a File. In this output, we searched for the word ‘the’ in a text file. The result showed that ‘the’ has appeared 4 times in a text file.

python count specific word
Python Count Specific Words in File

Read: Python get all files in directory

Python Count Words in Multiple Files

In this section, we will learn about Python Count Words in Multiple Files. We have three text files that we are going to use and we will count words from all of these files.

  • Counting words from multiple files can be done in five easy steps:
    • import glob module in Python
    • create an empty list to store text files and a counter with 0 as default value.
    • start a loop, recognize the text file using glob and add it to emty list we created in previous step.
    • start another loop on that empty list, total number of files will decide the number of times loop will run. Each time loop runs a file is opened, read, splitted in words and then length of total words in added to words variable.
    • In the end print the word variable with descriptive message.
  • glob is used to return all file with a specific extension. Since we need all the files with .txt extension so we have used glob here.
  • text_file=[] this empty list will store all the files with .txt extension. word=0 this will keep a track of all the words in multiple files.
for file in glob.glob("*.txt"):
    txt_files.append(file)
  • In this code, we have started a loop and glob is used to scan all the files with .txt extension.
  • each file is added to an empty list. So everytime loop runs a filename from the current folder having txt extension is added to an empty list.
for f in txt_files:
    file = open(f, "r")
    read_data = file.read()
    per_word = read_data.split()
    words += len(per_word)
  • In this code we have started a loop on the empty list because not that empty list has all the text files in it.
  • Each time loop runs a file is opened, read, all the sentences are splitted in words and total count of words are added to a variable.
  • In this way lets say file one has 20 words and file two has 30 then the words variable will show 50 (20+30) words in the end of the loop.
  • print('Total Words:',words) total words are printed with descriptive message.

Source Code:

Here is the source code to implement Python Count Words in Multiple Files.

import glob

# empty list and variable
txt_files = []
words = 0

# loop to add text files to a list
for file in glob.glob("*.txt"):
    txt_files.append(file)

# loop to read, split and count word of each file
for f in txt_files:
    file = open(f, "r")
    read_data = file.read()
    per_word = read_data.split()
    words += len(per_word)

# print total words in multiple files
print('Total Words:',words)

Output:

Here is the output of the above source code to implement Python Count Words in Multiple Files.

python count words from multiple files
Python Count Words in Multiple Files

Read: Python dictionary of lists

Python Count Unique Words in a File

In this section, we will learn about Python Count Unique Words in a File. The python program will check the occurrences of each word in a text file and then it will count only unique words in a file.

  • Using Python we can count unique words from a file in six simple steps:
    • create a counter and assign default value as zero
    • open a file in read only mode.
    • read the data of file
    • split the data in words and store it in a set
    • start a for loop and keep on incrementing the counter with each word.
    • Ultimately, print the counter
  • count = 0 is the counter with default value set to zero. This counter will increment later.
  • file = open("names.txt", "r") in this code we are opening a text file in a read-only mode and information is stored in a file variable.
  • read_data = file.read() in this code, we are reading the data stored in a file.
  • words = set(read_data.split()) In this code, we have split the data and also we have removed the duplicate values. Set always keep only unique data.
  • we have started a for loop on total words and each time the loop runs it adds one to the counter. So if there are 35 unique words then loop will run 35 times and counter will have 35.
  • In the end, count is printed as an output.

Source Code:

Here is the source code for implementing Python Count Unique Words in a File.

count = 0
file = open("names.txt", "r")
read_data = file.read()
words = set(read_data.split())
for word in words:
    count += 1
    
print('Total Unique Words:', count)

Output:

Here is the output of a program to count unique words in a file using Python. In this output, we have read a file and it has 85 unique words in it.

python count unique words
Python Count Unique Words File

Read: Python Dictionary to CSV

Python Count Words in Excel File

In this section, we will learn about Python Count Words in Excel File.

  • The best way to count words in excel files using python is by using Pandas module in python.
  • you need to install pandas on your device
# anaconda 
conda install pandas

# pip
pip install pandas
  • Using df.count() method in pandas we can count the total number of words in a file with columns.
  • Using df.count().sum() we can get the final value of total words in a file.
  • Here is the implementation on Jupyter Notebook.

Read: Pandas in Python

Python Count Unique Words in Text File

n this section, we will learn about Python Count Unique Words in a File. The python program will check the occurrences of each word in a text file and then it will count only unique words in a file.

  • Using Python we can count unique words from a file in six simple steps:
    • create a counter and assign default value as zero
    • open a file in read only mode.
    • read the data of file
    • split the data in words and store it in a set
    • start a for loop and keep on incrementing the counter with each word.
    • Ultimately, print the counter
  • count = 0 is the counter with default value set to zero. This counter will increment later.
  • file = open("names.txt", "r") in this code we are opening a text file in a read-only mode and information is stored in a file variable.
  • read_data = file.read() in this code, we are reading the data stored in a file.
  • words = set(read_data.split()) In this code, we have split the data and also we have removed the duplicate values. Set always keep only unique data.
  • we have started a for loop on total words and each time the loop runs it adds one to the counter. So if there are 35 unique words then loop will run 35 times and counter will have 35.
  • In the end, count is printed as an output with the descriptive message.

Source Code:

Here is the source code to implement Python Count Unique Words in Text File.

count = 0
file = open("names.txt", "r")
read_data = file.read()
words = set(read_data.split())
for word in words:
    count += 1
    
print('Total Unique Words:', count)

Output:

Here is the output of a program to count unique words in a file using Python. In this output, we have read a file and it has 85 unique words in it.

python count unique words
Python Count Unique Words in Text File

Read: Python Pandas CSV

Python Program to Count Number of Words in File

In this section, we will learn about python count words in files. In other words, we will learn to count the total number of words from a text file using Python.

  • The entire process is divided into three simple steps:
    • open a text file in read only mode
    • read the information of file
    • split the sentences into words and find the len.
  • Using file = open('file.txt', 'r') we can open the file in a read-only mode and store this information in a file variable.
  • read_data = file.read() this statement is used to read the entire data in one go and store it in a variable named read_data.
  • It’s time to split the sentences into words and that can be done using
    per_word = read_data.split() here split() method is used to split each sentence in read_data and all this information is stored in a variable named per_word.
  • final step is to print the length of per_word variable. Please note that lenght is counting total words in the file. Here is the statement to print a message with total count of words print('Total Words: ', len(per_word)).

Source Code:

Here is the source code to implement the Python Count Words in a File.

file = open("file.txt", "r")
read_data = file.read()
per_word = read_data.split()

print('Total Words:', len(per_word))

Output:

Here is the output of counting words in a file using Python. In this output, the text file we have used has 221 words.

python count words in a file

Read: Python built-in functions 

Python Count Word Frequency in a File

In this section, we will learn about Python Count Word Frequency in a File. In other words, we will count the number of times a word appeared in the file.

  • Frequency of each word can be counted in 3 simple steps in Python.
    • Import counter from collections module in python.
    • create a function that accepts filename, inside the function open the file, read the data and split sentences to words. and keep all of these inside the counter method.
    • call the function and print it with descriptive message.
  • In this we have imported Counter from collections. Counter holds the data in key-vaue format. Dictionary format would be best to display name and their occurrences.
  • in the function count_word(), we have opened the textfile and then returned each word with their total occurrences
  • In the end we have called the function and print it with descriptive message.

Source Code:

Here is the source code to implement Python Count Word Frequency in a File.

from collections import Counter

def count_word(file_name):
        with open(file_name) as f:
                return Counter(f.read().split())

print("Frequency :",count_word("names.txt"))

Output:

In this output, each word is displayed with their total occurrences in Python.

python count word frequency
Python Count Word Frequency in a File

Read: Get current directory Python

Python Word Count CSV File

In this section, we will learn about Python Word Count in CSV files.

  • The best way to count words in excel files using python is by using Pandas module in python.
  • you need to install pandas on your device.
# anaconda 
conda install pandas

# pip
pip install pandas
  • df.count()method in pandas we can count the total number of words in a file with columns.
  • Using df.count().sum() we can get the final value of total words in a file.
  • Here is the implementation on Jupyter Notebook.

You may also like to read the following articles.

In this tutorial, we have learned about Python Count Words in File. Also, we have covered these topics.

  • Python Count Words in File
  • Python Count Specific Words in File
  • Python Count Words in Multiple Files
  • Python Count Unique Words Files
  • Python Count Words in Excel File
  • Python Count Unique Words in Text File
  • Python Program to Count Number of Words in File
  • Python Count Word Frequency in a File
  • Python Word Count CSV File