Computer Science
Write a function AMCount() in Python, which should read each character of a text file STORY.TXT, should count and display the occurrence of alphabets A and M (including small cases a and m too).
Example :
If the file content is as follows :
Updated information
As simplified by official websites.
The EUCount() function should display the output as :
A or a : 4
M or m : 2
Answer
Let the file "STORY.TXT" include the following sample text:
Updated information
As simplified by official websites.
def AMCount(file_path):
count_a = 0
count_m = 0
with open(file_path, 'r') as file:
ch = ' '
while ch:
ch = file.read(1)
ch_low = ch.lower()
if ch_low == 'a':
count_a += 1
elif ch_low == 'm':
count_m += 1
print("A or a:", count_a)
print("M or m:", count_m)
AMCount("STORY.TXT")
Output
A or a: 4
M or m: 2
Related Questions
A file contains a list of telephone numbers in the following form:
Arvind 7258031 Sachin 7259197
The names contain only one word, the names and telephone numbers are separated by white spaces. Write program to read a file and display its contents in two columns.
Write a program to count the words "to" and "the" present in a text file "Poem.txt".
Write a program to count the number of upper-case alphabets present in a text file "Article.txt".
Write a program that copies one file to another. Have the program read the file names from user ?