You are on page 1of 1

FILE HANDLING - PROGRAM 2

QUESTION:
Write a function in python that counts the number of “Me” or “My” (in smaller
cases also), words present in a text file ‘story.txt’. The content of ‘story.txt’ is as
follows:
My first book was me and my family. It gave me chance to know the world.
CODE:
fp=open("story.txt")
data=fp.read()
def reader(data):
a=b=0
text=data.lower()
L=text.split()
for i in L:
if i=='me':
a+=1
if i=='my':
b+=1
print('Me occurs:',a,'times')
print('My occurs:',b,'times')
reader(data)

OUTPUT:
Me occurs: 2 times
My occurs: 2 times

You might also like