Text file
1. Write a method in Python to read lines from a text file INDIA.TXT, to find and
display the occurrence of the word 'India'.
Ans:
def display():
f = open('INDIA.txt','r')
a = f.readlines() # list of strings ['INDIA IS MY COUNTRY.\n', 'I LOVE INDIA']
print(a)
c=0
for line in a:
print(line)
if 'INDIA' in line:
c=c+1
print('occurrence of the word INDIA: ',c)
f.close()
display()
2. Write a Python function which reads a text file “poem.txt”
and prints the number of vowels in each line of that file,
separately.
Ans:
def show():
f = open('poem.txt','r')
a = f.read()
c=0
vowels ='aeiou'
for char in a:
if char in vowels:
c=c+1
print('no of the vowels: ',c)
show()
3. Write a method/function BIGLINES() in Python to read
lines from a text file CONTENT.TXT, and display those
lines, which are bigger than 20 characters.
Ans:
def BIGLINES():
f = open('CONTENT.txt','r')
a = f.readlines()
for line in a:
if len(line) > 20:
print(line)
BIGLINES()
4. Write a function COUNTTEXT( ), which reads a text file
Book.txt and displays all the words of the file whose length
is more than 3 or those which start with „ A‟ or „ a‟ . in the
form of a list. For example, if the Book.txt file contains
India is my country. They are studying.
then the output should be: [“India”, “country”, “They”,
“are”, “studying”]
Ans:
def COUNTTEXT():
f = open('counttext.txt','r')
words = f.read().split()
list =[]
for word in words:
if len(word) > 3 or word[0] =='A' or word[0]== 'a':
list.append(word)
print(list)
COUNTTEXT()
5. Write a Python program that writes the reverse of each line
in “input.txt” to another text file “output.txt”. Eg: if the
content of input.txt is:
The plates will still shift
and the clouds will still spew.
The content of “output.txt” should be:
tfihs llits lliw setalp ehT
.weps llits lliw sduolc eht dna
Ans:
f1 = open('input.txt','r')
f2 = open('output.txt','w')
lines = f1.readlines()
print(lines)
for line in lines:
#print(line)
reverse =''
for char in line: # The
reverse= char + reverse #ehT
print(reverse)
f2.write(reverse)
f2.write('\n')
f2.close()
f1.close()
6. Write a function named COUNT_CHAR() in python to count
and display number of times the arithmetic operators
(+,-,*,/) appears in the file “Math.txt” .
Example:
Solve the following:
1.(A+B)*C/D
2.(A-B)*(A+B)/D-(E/F)
3. A+B+C/D*(E/F)
The function COUNT_CHAR() must display the output as
Number of '+' sign is 4 Number of '-' sign is 2
Number of '*' sign is 3 Number of '/' sign is 5
Ans:
def COUNT_CHAR():
f = open('MATH.txt','r')
a,s,m,d = 0,0,0,0
w = f.read()
for letter in w:
if letter == '+':
a+=1 #a = a + 1
elif letter == '-':
s+=1
elif letter == '*':
m+=1
elif letter == '/':
d+=1
print('No of + sign = ',a)
print('No of - sign = ',s)
print('No of * sign = ',m)
print('No of / sign = ',d)
COUNT_CHAR()
7. Write a function V_COUNT() to read each line from the text file and count number of lines
begins and ends with any vowel.
Ans:
def V_COUNT():
a = f.readlines()
print(a)
c=0
vowels = 'aeiou'
for i in a:
if i[-1] == '\n':
if i[0] in vowels and i[-2] in vowels:
c+=1
elif i[0] in vowels and i[-1] in vowels:
c+=1
print("no of vowels begins and end in a line= ",c)
f = open('a.txt','r')
V_COUNT()
8. Write a user-defined function named Count() that will read
the contents of text file named“Report.txt” and count the
number of lines which starts with either "I" or "M".
E.g. In the following paragraph, there are 2 lines starting
with "I" or "M".
Ans:
def Count():
a = f.readlines()
c=0
for i in a:
if i[0] == "I" or i[0] == "M":
c+=1
print("The no of lines starts with I or M =",c)
f = open('Report.txt','r')
Count()
9. Write a function/method DISPLAYWORDS( ) in python to
read lines from a text file STORY.TXT and display those
words, which are less than 4 characters.
Ans:
def DISPLAYWORDS():
a = f.read().split()
c=0
for i in a:
if len(i) > 4:
c+=1
print('no of words less than 4 characters =',c)
f = open('STORY.txt','r')
DISPLAYWORDS()
(or)
def DISPLAYWORDS():
lines = f.readlines() #['It rained yesterday\n', 'yIt might rain today\n', 'I wish it rains tomorrow too\n
'I love Rain']
#print(lines)
c=0
for line in lines: #'It rained yesterday\n',
words = line.split()
print(words) # ['It', 'rained', 'yesterday']
for word in words:
if len(word) < 4:
c+=1
print('no of words less than 4 characters =',c)
f = open('STORY.txt','r')
DISPLAYWORDS()
10. Write a function stats( ) that accepts a filename and reports the file‟s longest line.
Ans:
def stats(f):
a =f.readlines()
maxi =len(a[0])
for line in a:
if len(line) > maxi:
long_line = line
maxi = len(line)
print(long_line)
print(maxi)
f = open('long.txt','r')
stats(f)
11. Write a function countdigits() in Python, which should read each character of a text file
"marks.txt", count the number of digits and display the file content and the number of digits.
Example: If the "marks.txt" contents are as follows:
Harikaran:40,Atheeswaran:35,Dahrshini:30,Jahnavi:48
The output of the function should be:
('Total number of digits in the file :', 8)
Ans:
def countdigits():
f = open('marks.txt','r')
r = f.read()
c =0
for i in r:
if i.isdigit():
c += 1
print('Total number of digits in the file :', c)
countdigits()
12. Write a function in Python to count the number of lines in a text file 'STORY.TXT'which is
starting with an alphabet 'A'.
Ans:
def countno():
f = open('STORY.txt','r')
a = f.readlines()
c=0
for i in a:
if i[0] == "A":
c+=1
print("no of lines starting with A = ",c)
countno()
13. Write a method SHOWLINES() in Python to read lines from
text file TESTFILE.TXT„ and display the lines which do not
contain 'ke'.
Ans:
def SHOWLINES():
f = open('TESTFILE.txt','r')
a = f.readlines()
k = 'ke'
for i in a:
if k not in i:
print(i)
SHOWLINES()
14. Write a function RainCount() in Python, which should read the content of a text file ―
TESTFILE.TXT ‖ and then count and display the count of occurrenceof word RAIN
(case_insensitive) in the file.
Example: If the file content is as follows:
It rained yesterday
It might rain today
I wish it rains tomorrow too
I love Rain
The RainCount() function should display the output as:
Rain – 2
Ans:
def RainCount():
f = open('TESTFILE.txt','r')
r = f.read().split()
c =0
for i in r:
if i=='rain' or i =='Rain':
c += 1
print('Rain –', c)
RainCount()
15. Define a function SHOWWORD () in python to read lines from a text file STORY.TXT, and
display those words, whose length is less than 5.
Ans:
def SHOWWORD():
f = open('STORY.txt','r')
a = f.read().split()
for i in a:
if len(i) < 5:
print(i)
SHOWWORD()
16. Write a user defined function in python that displays the
number of lines starting with 'H' in the filepara.txt
Ans:
def displayno():
f = open('filepara.txt','r')
a = f.readlines()
c=0
for i in a:
if i[0] == "H":
print(i)
c+=1
print("no of lines starting with H = ",c)
displayno()
17. Write a function in Python to print those words which
contains letter „ S‟ or „ s‟ anywhere in the word in text file
“STORY.txt”
Ans:
def Count():
f = open('STORY.txt','r')
r = f.read().split()
for i in r:
if 's' in i or 'S' in i:
print(i)
Count()
18. Write a function COUNTLINES( ) which reads a text file
STORY.TXT and then count and display the number of the
lines which starts and ends with same letter irrespective of
itscase.
Ans:
def COUNTLINES():
f = open('STORY.txt','r')
r = f.readlines()
c =0
for i in r:
a=i[0]
b=i[-2]
if a == b :
c += 1
print('No of lines starts and ends with same letter ', c)
COUNTLINES()