Text Files
Text Files
WAP to read the content of file and count how many times letter
“a” comes in file.
def cnt_a():
x=input('Enter file name')
f=open(x,'r')
cnt=0
s=f.read()
for i in s:
if i=='a':
cnt+=1
print('The no of "a" in the file is',cnt)
f.close()
cnt_a()
WAP to read the content of file and display ‘T’ in place of 'E'
while displaying the content of file all other characters should
appear as it is.
def replace():
x=input('Enter file name')
f=open(x,'r')
s=f.read()
for i in s:
if i=='E':
print('T',end='')
else:
print(i,end='')
f.close()
replace()
WAP to read the content of file and display how many upper case
characters and digits are present.
def up_num():
x=input('Enter file name')
cnt=0
cno=0
f=open(x,'r')
s=f.read()
for i in s:
if i.isupper()==True:
cnt+=1
elif i.isdigit()==True:
cno+=1
print('The number of digits are',cno)
print('The number of uppercase letters are',cnt)
f.close()
up_num()
WAP to read the content of file and count how many vowels in it.
def vow():
x=input('Enter file name')
cnt=0
f=open(x,'r')
s=f.read()
for i in s:
if i.lower() in 'aeoiu':
cnt+=1
print('The no. of vowels in the file are',cnt)
f.close()
vow()
Write a function ISTOUPCOUNT() in python to read the content of
file WRITER.TXT, to count and display the occurance of IS, TO,
UP.
def ISTOUPCOUNT():
x=input('Enter file name')
cn1,cn2,cn3=0,0,0
f=open(x,'r')
s=f.read()
s=s.split(' ')
for i in s:
if i.lower()=='is':
cn1+=1
elif i.lower()=='to':
cn2+=1
elif i.lower()=='up':
cn3+=1
print('The number of "is"=',cn1,'The number of "to"=',cn2,'The
number of "up"=',cn3)
f.close()
ISTOUPCOUNT()
def COUNTAE():
f=open('WRITER.txt','r')
while True:
s=f.readline()
if not s:
break
if s[0].upper() in 'AE':
print(s)
f.close()
COUNTAE()
Read a file given and display the frequency of alphabets,
vowels, consonants, digits, upper, lower, space and special
characters.
def group():
x=input('Enter file name')
f=open(x,'r')
s=f.read()
cSpl,cS,cD,cV,cC,cL,cU,cA=0,0,0,0,0,0,0,0
for i in s:
if i.isalpha()==True:
cA+=1
if i.isupper()==True:
cU+=1
if i in 'AEIOU':
cV+=1
else:
cC+=1
else:
cL+=1
if i in 'aeiou':
cV+=1
else:
cC+=1
elif i.isdigit()==True:
cD+=1
elif i.isspace()==True:
cS+=1
else:
cSpl+=1
print('count of alphabets=',cA)
print('count of upper=',cU)
print('count of lower=',cL)
print('count of consonants=',cC)
print('count of vowels=',cV)
print('count of digits=',cD)
print('count of space=',cS)
print('count of special characters=',cSpl)
f.close()
group()
WAF that accepts a word from user. Read a file and append to
index.txt storing the given word and line number
WAF to print those lines starting with articles. Also print the
count