Text File Question Bank Solutions
Text File Question Bank Solutions
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')
print(a)
c=0
for line in a:
print(line)
if 'INDIA' in line:
c=c+1
f.close()
display()
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
show()
Ans:
def BIGLINES():
f = open('CONTENT.txt','r')
a = f.readlines()
for line in a:
print(line)
BIGLINES()
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
“are”, “studying”]
Ans:
def COUNTTEXT():
f = open('counttext.txt','r')
words = f.read().split()
list =[]
list.append(word)
print(list)
COUNTTEXT()
Ans:
f1 = open('input.txt','r')
f2 = open('output.txt','w')
lines = f1.readlines()
print(lines)
#print(line)
reverse =''
print(reverse)
f2.write(reverse)
f2.write('\n')
f2.close()
f1.close()
Example:
2.(A-B)*(A+B)/D-(E/F)
3. A+B+C/D*(E/F)
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
s+=1
m+=1
d+=1
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':
c+=1
c+=1
f = open('a.txt','r')
V_COUNT()
Ans:
def Count():
a = f.readlines()
c=0
for i in a:
c+=1
f = open('Report.txt','r')
Count()
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
words = line.split()
if len(word) < 4:
c+=1
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:
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.
Harikaran:40,Atheeswaran:35,Dahrshini:30,Jahnavi:48
Ans:
def countdigits():
f = open('marks.txt','r')
r = f.read()
c =0
for i in r:
if i.isdigit():
c += 1
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
countno()
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.
It rained yesterday
I love Rain
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()
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
displayno()
“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()
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
COUNTLINES()