1.
Program to read a complete Text file
f=open("c:\\my\\
try.txt","r")
s=f.read()
print(s)
f.close( )
2. Program to read a file and count
total no. of blanks in it
f=open("myfile.txt",'r')
c=0
s=' '
while s:
s=f.read(1)
print(s,end="")
if s==' ':
c=c+1
print("Total no. of blanks are :",c)
f.close( )
3. Program to read a file and count
total no. of lower case vowels in it
f=open("myfile.txt",'r')
c=0
s=' '
while s:
s=f.read(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)
f.close( )
4. Program to read and count no.
of lines beginning with ‘A’.
f=open("myfile.txt",'r')
s=" "
c=0
while s:
z=s[0]
s=f.readline()
print(s,end="")
if z==‘A':
c=c+1
print(c)
f.close( )
5. Program to read a file and count
total no. of 'am' in it.
f=open("myfile.txt",'r')
c=0
s=' '
while s:
s=f.readline()
a=s.count('am')
c=c+a
print("\nTotal no. of 'am' are :",c)
f.close( )
6. Program to count total no. of lines
which have 3 words in it.
f=open("myfile.txt",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)
f.close()
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("file1.txt",'r')
f2=open("file2.txt",'w')
c=0
s=' '
while s:
s=f1.read(1)
if s=='a' or s=='e' or s=='i' or s=='o' or s=='u':
c=c+1
f2.write(s.upper())
else:
f2.write(s)
print("\nTotal no. of vowels copied are :",c)
f1.close()
f2.close()
f3=open("file2.txt",'r')
print("\nContents of the copied file are :")
s=f3.read()
print(s)
f3.close()
8. Program to find sum of all digits
in a file.
f1=open("file1.txt",'r')
sum1=0
s=' '
while s:
s=f1.read(1)
if s.isdigit():
sum1=sum1+int(s)
print("\nSum of all digits in the file is:",sum1)
f1.close()
9. Program to reverse , count and
print all lines which begins with
'I’ of a text file.
f=open("myfile.txt",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)
f.close()
10.Program to replace every lower
case vowel with upper case vowel.
f1=open("myfile.txt",'r')
f2=open("temp.txt",'w')
s=' '
while s:
s=f1.read(1)
if s=='a' or s=='e' or s=='i' or s=='o' or s=='u':
f2.write(s.upper())
else:
f2.write(s)
f1.close()
f2.close()
f3=open("temp.txt",'r')
print("\nContents of new file are:\n")
s=f3.read()
print(s)
11. Program to read a file and
separate every word with #.
f=open("myfile.txt",mode='r')
c=0
for i in f:
s=i.split()
for j in s:
print(j,"#",end=" ")
print()
f.close()
12. Program to count total number
of words which ends with ‘ly’ in a
text file.
f=open("myfile.txt",'r')
c=0
for i in f:
s=i.split()
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)
f.close()
13. Write a menu driven Program
to read a file name.txt.
#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=f.read(1)
print(s,end="")
except EOFError:
break
f.close()
def Word(F1):
f=open(F1,mode='r')
print("File Contents word by word:")
for i in f:
s=i.split()
for j in s:
print(j,end=" ")
print()
f.close()
def Line(F1):
f=open(F1,mode='r')
print("File Contents Line by Line:")
for i in f:
print(i)
f2.close()
F1=input("Enter File Name")
print("1.Read File Character by Character")
print("2.Read File Word by Word ")
print("3.Read 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=f1.read(1)
if s.isalpha():
sum1=sum1+1
print("Total number of Alphabets are: ",sum1)
f1.close()
def Digits(F):
f1=open(F,'r')
sum1=0
s=' '
while s:
s=f1.read(1)
if s.isdigit():
sum1=sum1+1
print("Total number of Digits are: ",sum1)
f1.close()
def Words(F):
f1=open(F,'r')
sum1=0
s=" "
for i in f1:
s=i.split()
sum1=sum1+len(s)
print("Total number of Words are: ",sum1)
f1.close()
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=f1.read(1)
if s==' ':
sum1=sum1+1
print("Total number of Spaces are: ",sum1)
f1.close()
def char(F):
f1=open(F,'r')
sum1=0
s=' '
while s:
s=f1.read(1)
if s==' ':
sum1=sum1+1
sum1=f1.tell()-sum1
print("Total number of Characters are: ",sum1)
f1.close()
def Vowels(F):
f1=open(F,'r')
sum1=0
s=' '
while s:
s=f1.read(1)
if s in "aeiou":
sum1=sum1+1
print("Total number of Vowels are : ",sum1)
f1.close()
def WordsA(F):
f1=open(F,'r')
sum1=0
s=" "
for i in f1:
s=i.split()
for j in s:
if j[0]=='A':
sum1=sum1+1
print("Total number of Words beginning with 'A' are: ",sum1)
f1.close()
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 original.txt to
backup.txt.
def copy ( ) :
file1=open("original.txt",'r')
file2=open("backup.txt",'w')
c=0
s=' '
for z in file1:
s=z.split()
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
file2.write(s[i])
file2.write(" ")
print("\nTotal no. of copied words are :",c)
file1.close()
file2.close()
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 old.txt are :
i Am A STUdent.I LIKE Maths.he likes English.
Then contents of new.txt should be :
I am a student.I like maths.He likes english.
file1=open("old.txt",'r')
file2=open("new.txt",'w')
c=0
T=' '
T=file1.read(1)
file2.write(T.upper())
while T:
T=file1.read(1)
if T=='.':
file2.write(T)
T=file1.read(1)
file2.write(T.upper())
elif T.isupper():
file2.write(T.lower())
else:
file2.write(T)
file1.close()
file2.close()
file3=open("new.txt",'r‘)
print("\nContents of the new file are :")
T=' '
while T:
T=file3.read()
print(T,end=" ")
file3.close()
18. WAP to
#Copy all vowels in one.txt
#Copy all consonants in two .txt
# Copy all other characters in
three .txt
from original.txt
file1=open("original.txt",'r')
file2=open(“one.txt",'w')
file3=open(“two.txt",‘w')
file4=open(“three.txt",'w')
c1,c2,c3,c4=0,0,0,0
s=' '
while s:
s=file1.read(1)
if s.isalpha() and s in “AEIOUaeiou”:
c1=c1+1
file2.write(s)
elif s.isalpha() and s not in “AEIOUaeiou”:
c2=c2+1
file3.write(s)
else:
c3=c3+1
file4.write(s)
print("Total vowels : ",c1)
print("Total consonants : ",c2)
print("Total other characters : ",c3)
print("Total letters: ",c1+c2+c3)
file1.close()
file2.close()
file3.close()
file4.close()