Python Count Each Vowel in Text File Program

By | May 22, 2021

Python Count Each Vowel in Text File Program – In this File Handling tutorial, we will discuss a Python File Program to show count of each vowel in the given text file.

Python find count of each vowel in text file

How This Program Works?

First of all, we have to create a text file with name mytextfile.txt in a textfile folder of D drive. You can use note pad or any other similar text editor to create the required text file with some text data.

Python program will start by opening the text file in read mode as follows:

file1 = open("d:/textfile/mytextfile.txt", "r")

After opening the text file we will read the entire text file and place the text in a variable:

text_file = file1.read()

Next, we will initialize all counter variables for counting vowels:

vowel_count =  0
A_count = 0
E_count = 0
I_count = 0
O_count = 0
U_count = 0
a_count = 0
e_count = 0
i_count = 0
o_count = 0
u_count = 0

Now we will use a loop to read each character from the string variable and check it for a vowel and count it accordingly.

for ch in text_file:
    # Count each upper case vowel
    if(ch  == 'A'):
        A_count = A_count +1       
    if(ch  == 'E'):
        E_count = E_count +1       
    if(ch  == 'I'):
        I_count = I_count +1       
    if(ch  == 'O'):
        O_count = O_count +1       
    if(ch  == 'U'):
        U_count = U_count +1   
    # Count each lower case vowel
    if(ch  == 'a'):
        a_count = a_count +1       
    if(ch  == 'e'):
        e_count = e_count +1       
    if(ch  == 'i'):
        i_count = i_count +1       
    if(ch  == 'o'):
        o_count = o_count +1       
    if(ch  == 'u'):
        u_count = u_count +1      

		
    if( ch=='A' or ch=='a' or ch=='E' or ch=='e' or ch=='I' 
        or ch=='i' or ch=='O' or ch=='o'
	or ch=='U' or ch=='u'):
        	vowel_count +=1

At the end, we will display the values of all counter variables as follows:

print('The Number of Vowels in text file :', vowel_count)
print('The Number of Vowel "A" in text file :', A_count)
print('The Number of Vowel "E" in text file :', E_count)
print('The Number of Vowel "I" in text file :', I_count)
print('The Number of Vowel "O" in text file :', O_count)
print('The Number of Vowel "U" in text file :', U_count)

print('The Number of Vowel "a" in text file :', a_count)
print('The Number of Vowel "e" in text file :', e_count)
print('The Number of Vowel "i" in text file :', i_count)
print('The Number of Vowel "o" in text file :', o_count)
print('The Number of Vowel "u" in text file :', u_count)

After displaying the output, we will use the close() function to close the file properly.

file1.close()

Source Code Python File Program Example – Find number of each vowel in a text file.

# Python File Program to count
# number of each vowel in a Text File


# Write a python program to
# open a file in read mode
# and count number of vowels
# in this text file
# Assume that the text file has the name
# mytextfile.txt and is in textfile folder on D drive
# Assume that mytextfile.txt has the 
# following contents without hash sign

# Python File Example Programs on EasyCodeBook.com

# open file in read mode
file1 = open("d:/textfile/mytextfile.txt", "r")

# read all the contents of file
# and store in a string variable 
# named text_file using file read() function

text_file = file1.read()

# find the number of each vowel in text file

# initialize counter variables for each vowel
vowel_count =  0
A_count = 0
E_count = 0
I_count = 0
O_count = 0
U_count = 0
a_count = 0
e_count = 0
i_count = 0
o_count = 0
u_count = 0

for ch in text_file:
    # Count each upper case vowel
    if(ch  == 'A'):
        A_count = A_count +1       
    if(ch  == 'E'):
        E_count = E_count +1       
    if(ch  == 'I'):
        I_count = I_count +1       
    if(ch  == 'O'):
        O_count = O_count +1       
    if(ch  == 'U'):
        U_count = U_count +1   
    # Count each lower case vowel
    if(ch  == 'a'):
        a_count = a_count +1       
    if(ch  == 'e'):
        e_count = e_count +1       
    if(ch  == 'i'):
        i_count = i_count +1       
    if(ch  == 'o'):
        o_count = o_count +1       
    if(ch  == 'u'):
        u_count = u_count +1      

		
    if( ch=='A' or ch=='a' or ch=='E' or ch=='e' or ch=='I' 
        or ch=='i' or ch=='O' or ch=='o'
	or ch=='U' or ch=='u'):
        	vowel_count +=1
        

print('The Number of Vowels in text file :', vowel_count)
print('The Number of Vowel "A" in text file :', A_count)
print('The Number of Vowel "E" in text file :', E_count)
print('The Number of Vowel "I" in text file :', I_count)
print('The Number of Vowel "O" in text file :', O_count)
print('The Number of Vowel "U" in text file :', U_count)

print('The Number of Vowel "a" in text file :', a_count)
print('The Number of Vowel "e" in text file :', e_count)
print('The Number of Vowel "i" in text file :', i_count)
print('The Number of Vowel "o" in text file :', o_count)
print('The Number of Vowel "u" in text file :', u_count)

file1.close()

Output from a Sample Run

As the text in the file is:
Python File Example Programs on EasyCodeBook.com

The Number of Vowels in text file : 16
The Number of Vowel "A" in text file : 0
The Number of Vowel "E" in text file : 2
The Number of Vowel "I" in text file : 0
The Number of Vowel "O" in text file : 0
The Number of Vowel "U" in text file : 0
The Number of Vowel "a" in text file : 3
The Number of Vowel "e" in text file : 3
The Number of Vowel "i" in text file : 1
The Number of Vowel "o" in text file : 7
The Number of Vowel "u" in text file : 0

Python may show File Not Found error

If you do not create a text file with correct name and path as written in this Python program, there will be an error as follows:

Traceback (most recent call last):
  File "C:/Users/h/AppData/Local/Programs/Python/Python37-32/count_each_vowel_file.py", line 17, in 
    file1 = open("d:/mytext.txt", "r")
FileNotFoundError: [Errno 2] No such file or directory: 'd:/mytext.txt'

Perfect Python File Handling Tutorial With More Than 10 Practical Example Programs

 395 total views,  8 views today

Leave a Reply

Your email address will not be published. Required fields are marked *