1.
Program to read a complete Text file
f=open("c:\\my\\
[Link]","r")
s=[Link]()
print(s)
[Link]( )
2. Program to read a file and count
total no. of blanks in it
f=open("[Link]",'r')
c=0
s=' '
while s:
s=[Link](1)
print(s,end="")
if s==' ':
c=c+1
print("Total no. of blanks are :",c)
[Link]( )
3. Program to read a file and count
total no. of lower case vowels in it
f=open("[Link]",'r')
c=0
s=' '
while s:
s=[Link](1)
print(s,end="")
if s=='a' or s=='e' or s=='i' or s=='o' or
s=='u':
c=c+1
print("Total no. of blanks are :",c)
[Link]( )
4. Program to read and count no.
of lines beginning with ‘A’.
f=open("[Link]",'r')
s=" "
c=0
while s:
z=s[0]
s=[Link]()
print(s,end="")
if z==‘A':
c=c+1
print(c)
[Link]( )
5. Program to read a file and count
total no. of 'am' in it.
f=open("[Link]",'r')
c=0
s=' '
while s:
s=[Link]()
a=[Link]('am')
c=c+a
print("\nTotal no. of 'am' are :",c)
[Link]( )
6. Program to count total no. of lines
which have 3 words in it.
f=open("[Link]",mode='r')
c,c1=0,0
for i in f:
T=len(i)
c=0
for j in range(0,T,1):
if i[j]==' ':
c=c+1
if c==2:
c1=c1+1
print(i)
print("\nTotal no. of lines which have 3 words :",c1)
[Link]()
7. Program to count and copy vowels
from lower case to upper case
from file1 to file2 and rest of the
contents are copied as they are.
f1=open("[Link]",'r')
f2=open("[Link]",'w')
c=0
s=' '
while s:
s=[Link](1)
if s=='a' or s=='e' or s=='i' or s=='o' or s=='u':
c=c+1
[Link]([Link]())
else:
[Link](s)
print("\nTotal no. of vowels copied are :",c)
[Link]()
[Link]()
f3=open("[Link]",'r')
print("\nContents of the copied file are :")
s=[Link]()
print(s)
[Link]()
8. Program to find sum of all digits
in a file.
f1=open("[Link]",'r')
sum1=0
s=' '
while s:
s=[Link](1)
if [Link]():
sum1=sum1+int(s)
print("\nSum of all digits in the file is:",sum1)
[Link]()
9. Program to reverse , count and
print all lines which begins with
'I’ of a text file.
f=open("[Link]",mode='r')
c=0
for i in f:
if i[0]=='I':
L=len(i)
for j in range(L-1,-1,-1):
print(i[j],end=" ")
c=c+1
print("\nTotal no. of lines are :",c)
[Link]()
[Link] to replace every lower
case vowel with upper case vowel.
f1=open("[Link]",'r')
f2=open("[Link]",'w')
s=' '
while s:
s=[Link](1)
if s=='a' or s=='e' or s=='i' or s=='o' or s=='u':
[Link]([Link]())
else:
[Link](s)
[Link]()
[Link]()
f3=open("[Link]",'r')
print("\nContents of new file are:\n")
s=[Link]()
print(s)
11. Program to read a file and
separate every word with #.
f=open("[Link]",mode='r')
c=0
for i in f:
s=[Link]()
for j in s:
print(j,"#",end=" ")
print()
[Link]()
12. Program to count total number
of words which ends with ‘ly’ in a
text file.
f=open("[Link]",'r')
c=0
for i in f:
s=[Link]()
for j in s:
L=len(j)
if j[L-2]=='l' and j[L-1]=='y':
c=c+1
print("\nTotal no. of word which ends with 'ly' are :",c)
[Link]()
13. Write a menu driven Program
to read a file [Link].
#character by character
#word by word
#line by line
def char(F1):
f=open(F1,mode='r')
c=0
s=' '
print("File Contents character by character:")
while s:
try:
s=[Link](1)
print(s,end="")
except EOFError:
break
[Link]()
def Word(F1):
f=open(F1,mode='r')
print("File Contents word by word:")
for i in f:
s=[Link]()
for j in s:
print(j,end=" ")
print()
[Link]()
def Line(F1):
f=open(F1,mode='r')
print("File Contents Line by Line:")
for i in f:
print(i)
[Link]()
F1=input("Enter File Name")
print("[Link] File Character by Character")
print("[Link] File Word by Word ")
print("[Link] File Line by Line")
ch=int(input("Enter your choice:"))
if ch==1:
char(F1)
elif ch==2:
Word(F1)
elif ch==3:
Line(F1)
else:
print("WRONG CHOICE")
14. Using user defined functions
WAP to
#Count number of alphabets
#Count number of digits
#Count number of words
in a text file.
def Alpha(F):
f1=open(F,'r')
sum1=0
s=' '
while s:
s=[Link](1)
if [Link]():
sum1=sum1+1
print("Total number of Alphabets are: ",sum1)
[Link]()
def Digits(F):
f1=open(F,'r')
sum1=0
s=' '
while s:
s=[Link](1)
if [Link]():
sum1=sum1+1
print("Total number of Digits are: ",sum1)
[Link]()
def Words(F):
f1=open(F,'r')
sum1=0
s=" "
for i in f1:
s=[Link]()
sum1=sum1+len(s)
print("Total number of Words are: ",sum1)
[Link]()
F=input("Enter File Name")
Alpha(F)
Digits(F)
Words(F)
15. Using user defined functions
WAP to
#Count number of spaces
#Count Number of characters
#Count the number of vowels
#Count the word start with capital ‘A’
def Spaces(F):
f1=open(F,'r')
sum1=0
s=' '
while s:
s=[Link](1)
if s==' ':
sum1=sum1+1
print("Total number of Spaces are: ",sum1)
[Link]()
def char(F):
f1=open(F,'r')
sum1=0
s=' '
while s:
s=[Link](1)
if s==' ':
sum1=sum1+1
sum1=[Link]()-sum1
print("Total number of Characters are: ",sum1)
[Link]()
def Vowels(F):
f1=open(F,'r')
sum1=0
s=' '
while s:
s=[Link](1)
if s in "aeiou":
sum1=sum1+1
print("Total number of Vowels are : ",sum1)
[Link]()
def WordsA(F):
f1=open(F,'r')
sum1=0
s=" "
for i in f1:
s=[Link]()
for j in s:
if j[0]=='A':
sum1=sum1+1
print("Total number of Words beginning with 'A' are: ",sum1)
[Link]()
F=input("Enter File Name")
Spaces(F)
char(F)
Vowels(F)
WordsA(F)
FOR REVISION WORK
16. Using a user defined function
write a program to copy and count all
words which do not begin with a
capital vowel from file [Link] to
[Link].
def copy ( ) :
file1=open("[Link]",'r')
file2=open("[Link]",'w')
c=0
s=' '
for z in file1:
s=[Link]()
L=len(s)
for i in range(0,L,1):
if s[i][0]!='A' and s[i][0]!='E' and s[i][0]!='I' and s[i][0]!='O' and s[i][0]!='U':
c=c+1
[Link](s[i])
[Link](" ")
print("\nTotal no. of copied words are :",c)
[Link]()
[Link]()
print(“Program to copy words:”)
copy ( )
17. Program to copy contents from old to new
file.
# first letter should be in capital
# every char. after a full stop must be in capitals
# rest all small
If contents of [Link] are :
i Am A STUdent.I LIKE [Link] likes English.
Then contents of [Link] should be :
I am a student.I like [Link] likes english.
file1=open("[Link]",'r')
file2=open("[Link]",'w')
c=0
T=' '
T=[Link](1)
[Link]([Link]())
while T:
T=[Link](1)
if T=='.':
[Link](T)
T=[Link](1)
[Link]([Link]())
elif [Link]():
[Link]([Link]())
else:
[Link](T)
[Link]()
[Link]()
file3=open("[Link]",'r‘)
print("\nContents of the new file are :")
T=' '
while T:
T=[Link]()
print(T,end=" ")
[Link]()
18. WAP to
#Copy all vowels in [Link]
#Copy all consonants in two .txt
# Copy all other characters in
three .txt
from [Link]
file1=open("[Link]",'r')
file2=open(“[Link]",'w')
file3=open(“[Link]",‘w')
file4=open(“[Link]",'w')
c1,c2,c3,c4=0,0,0,0
s=' '
while s:
s=[Link](1)
if [Link]() and s in “AEIOUaeiou”:
c1=c1+1
[Link](s)
elif [Link]() and s not in “AEIOUaeiou”:
c2=c2+1
[Link](s)
else:
c3=c3+1
[Link](s)
print("Total vowels : ",c1)
print("Total consonants : ",c2)
print("Total other characters : ",c3)
print("Total letters: ",c1+c2+c3)
[Link]()
[Link]()
[Link]()
[Link]()