Program:13
Read a text file and display the number of vowels/consonants/uppercase/lowercase characters in the file.
f=open("poem.txt","r") vowels='aeiouAEIOU' v=0 c=0 u=0 l=0 for i in f.read(): if i.isalpha(): if i.isupper(): u=u+1 else: l=l+1 if i in vowels: v=v+1 else: c=c+1 print("Vowels=",v) print("consonants=",c) print("uppercase letters=",u) print("lowercase letters=",l) f.close()
Program:14
Remove all the lines that contain the character ‘a’ in a file and write it to another file.
f=open("poem.txt","r") f1=open("new.txt","w") for line in f.readlines(): if 'a' not in line: f1.write(line) f.close() f1.close()
Program:15
Create a binary file with name and roll number. Search for a given roll number and display the name, if not found display appropriate message.
import pickle fh=open("student.dat","wb") lst=[] try: while True: r=int(input('enter roll no.: ')) n=input('enter name: ') lst=[r,n] pickle.dump(lst,fh) ch=input('do you want to cont') if ch=='Y' or ch=='y': continue else: break except EOFError: fh.close() fh=open("student.dat","rb") r=int(input('enter the roll number to search')) lst1=[] z=0 try: while True: lst1=pickle.load(fh) if lst1[0]==r: print(lst1[1]) z=1 except EOFError: if z==0: print('record not found') else: print('search successful') fh.close()