Python Text File Program To Count Vowels

By | August 14, 2019

Task: Write a Python Text File Program To Count Vowels. This Python Programming tutorial will explain how to open a text file in read mode. This Python script will count the how many vowels are present in this text file.

"Write

For this program we will assume that:

The file has the name mytext.txt and it resides on D drive. This Python program will use:

open() function
read() function
for loop
close() function

The Python Source Code – Write a Python Text File Program To Count Vowels

# 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
# mytext.txt and is on D drive
# Assume that mytext.txt file has the 
# following contents without hash sign

# This file contains some example text.

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

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

str1 = file1.read()

# find the number of the vowels in file

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

print('The Number of Vowels in text file :', vowel_count)

file1.close()

# The output is as follows
# The Number of Vowels in text file : 12

The output will be:

The Number of Vowels in text file : 12

You may also like to read:

Python File Programs – Python File Handling

Write a Python file program read numbers write squares.
Python Text File Read and Show Contents Program
Python Text File Read and Display file data with Exception Handling FileNotFoundError
Python File Program Count Characters in Text File
Python Text File Program To Count Vowels

Leave a Reply

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