Topic: Write a Python Program to Find Occurrences of Each Word in Text File Python Program

Source Code : Python Program using Text File
# Python program to count # number of occurences / frequencies of words in a text # Input filename, open it in # read mode. display its cntents # count and display number of occurrences of words # in this file # www.EasyCodeBook.com file_name = input("Enter file name:") file1 = open(file_name, "r") d = dict() print("\n File Contents are:\n") for line in file1: print(line, end='') line = line.strip() line = line.lower() words = line.split(" ") for word in words: if word in d: d[word] = d[word] + 1 else: d[word] = 1 print("\n\nNumber of occurrences of each word in given text file is:") print("\n ===============\n") for key in list(d.keys()): print(key, ":", d[key]) file1.close()
Python code with comments to explain the Logic of the Program: Find Occurrences of Each Word in Text File Python Program
# Python program to count # number of occurences / frequencies of words in a text # Input filename, open it in # read mode. display its cntents # count and display number of occurrences of words # in this file # www.EasyCodeBook.com # ask the user to enter filename file_name = input("Enter file name:") # open file for reading file1 = open(file_name, "r") # we will use a dictionary named d d = dict() print("\n File Contents are:\n") # Use a for loop to read each line one by one from the given text file # display file contents line by line, too for line in file1: print(line, end='') # use strip() method for removing any leading spaces and newline character line = line.strip() # we use lower() method to convert this line into lowercase line = line.lower() # we use split() method to split the line into words (use space as separator) # all words are in words list now words = line.split(" ") # we wll loop through every word in the line for word in words: # we check if the word is already in dictionary if word in d: # if word already present, increment count of word by 1 d[word] = d[word] + 1 else: # else if word not present in dictionary then add it to dictionary and now count =1 d[word] = 1 print("\n\nNumber of occurrences of each word in file is:") print("\n ===============\n") # use the for loop to show evey entry of dictionary # this will print number of occurrences of each word in # the given text file for key in list(d.keys()): print(key, ":", d[key]) # close the file file1.close()
Please, note that we are assuming that a text file with name “example.txt” with 3 lines of text is present on D drive before execution of this program.
this is example text file
this is line 2
this is last line
Sample Run:
Output:
Enter file name:d://example.txt
File Contents are:
this is example text file
this is line 2
this is last line
Number of occurrences of each word in file is:
===============
this : 3
is : 3
example : 1
text : 1
file : 1
line : 2
2 : 1
last : 1
You will also like the following Python File Programs:
- Count Words in Each Line of Text File Python Program
 - Count Words in Text File Python Program
 - Count Lines in Text File Python Program
 - Python Text File Program To Count Vowels
 - Python File Program Count Characters in Text File
 - Python Exception Handling FileNotFoundError
 - Python Text File Read and Show Contents Program
 - Python file program read numbers write squares
 
How this File Program works?
Python program to count the number of occurences / frequencies of words in a text file
- Input filename,
 - Open it in read mode.
 - Display its cntents
 - Count and display number of occurrences of words in this file
 - We will use a dictionary named d d = dict()
 - Use a for loop to read each line one by one from the given text file
 - Display file contents line by line, too
 - Use strip() method for removing any leading spaces and newline character
 - We use lower() method to convert this line into lowercase
 - We use split() method to split the line into words (use space as separator)
 - All words are in words list now
 - We will loop through every word in the line
 - We check if the word is already in dictionary
 - If word already present, increment count of word by 1
 - Else if word not present in dictionary then add it to dictionary and now count =1
 - Use the for loop to show evey entry of dictionary
 - This will print number of occurrences of each word in the given text file
 - Close the file.
 
 ![]()