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.
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
Perfect Python Files Tutorial with Theory and Solved 15+ Example File Programs in Python Programming Language
You may also like to read:
- Counting Words Occurrences in String Python Program
- Python File Copy Program using shutil.filecopy
- Find Occurrences of Each Word in Text File Python Program
- 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 String Program To Count Vowels
- Python Text File Program To Count Vowels
- Python File Program Count Characters in Text File
- Counting Vowels in String Java Program
- Java Vowel Count in String Program
- C Program Display and Count Odd Numbers in Array
- C Program Find Sum of Even Numbers in Array
- C Program Count Frequency of Each Element in Array
- Counting Words Occurrences in String Python Program
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
Pingback: Count Lines in Text File Python Program | EasyCodeBook.com