0% found this document useful (0 votes)
61 views4 pages

Text File Based Questions

The document contains a series of Python function definitions for various text file operations, including counting lines and words based on specific criteria, displaying certain lines, and filtering content. Functions are provided for counting occurrences of specific words, checking word lengths, and reading files while applying conditions. Each function is designed to handle different text files and perform specific tasks related to text processing.

Uploaded by

Lavanya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views4 pages

Text File Based Questions

The document contains a series of Python function definitions for various text file operations, including counting lines and words based on specific criteria, displaying certain lines, and filtering content. Functions are provided for counting occurrences of specific words, checking word lengths, and reading files while applying conditions. Each function is designed to handle different text files and perform specific tasks related to text processing.

Uploaded by

Lavanya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Text File – Exam based questions

Write a function in python to count the number Write a user defined function countwords() to
lines in a text file ‘Country.txt’ which is starting display the total number of words present in the
with an alphabet ‘W’ or ‘H’. file from a text file “Quotes.Txt”.
def count_W_H():
f = open (“Country.txt”, “r”) def countwords():
W,H = 0,0 s = open("Quotes.txt","r")
r = f.read() f = s.read()
for x in r: z = f.split ()
if x[0] == “W” or x[0] == “w”: count = 0
W=W+1 for i in z:
elif x[0] == “H” or x[0] == “h”: count = count + 1
H=H+1 print ("Total number of words:", count)
f.close()
print (“W or w :”, W)
print (“H or h :”, H)

Write a user defined function evencountwords() Write a function COUNT_AND( ) in Python to read
to display the total number of words which are the text file “STORY.TXT” and count the number
even in length present in the file from a text file of times “AND” occurs in the file. (include
“Quotes.Txt”. AND/and/And in the counting)
def evencountwords(): def COUNT_AND( ):
s = open("Quotes.txt","r") count=0
f = s.read() file=open(‘STORY.TXT','r')
z = f.split () line = file.read()
count = 0 word = line.split()
for i in z: for w in word:
if len(i) %2==0: if w ==’AND’:
count = count + 1 count=count+1
print ("Total number of even words:", count) print(count)
file.close()

Write a function DISPLAYWORDS( ) in python to Write a function that counts and display the
display the count of words starting with “t” or “T” number of 5 letter words in a text file “Sample.txt
in a text file ‘STORY.TXT’. def count_words( ):
def COUNT_AND( ): c=0
count=0 f = open("Sample.txt")
file=open(‘STORY.TXT','r') line = f.read()
line = file.read() word = line.split()
word = line.split() for w in word:
for w in word: if len(w) == 5:
if w[0] ==’t’ or w[0]==’T’: c += 1
count=count+1 print(c)
print(count)
file.close()
Write a function that counts and display the Write a function to display those lines which start
number of more than 5 letter words in a text file with the letter “G” from the text file
“Sample.txt “MyNotes.txt”
def count_words( ): def count_lines( ):
c=0 c=0
f = open("Sample.txt") f = open("MyNotes.txt")
line = f.read() line = f.readlines()
word = line.split() for w in line:
for w in word: if w[0] == 'G':
if len(w) >= 5: print(w)
c += 1 f.close()
print(c)
f.close()
Write a function in python to read lines from file Write a function COUNT() in Python to read
“POEM.txt” and display all those words, which contents from file “REPEATED.TXT”, to count and
has two characters in it. display the occurrence of the word “Catholic” or
def TwoCharWord(): “mother”.
f = open('poem.txt') def COUNT():
count = 0 f = open('REPEATED.txt')
s=f.readlines() count = 0
for line in s: s=f.readlines()
words = line.split() for line in s:
for w in words: words = line.split()
if len(w)==2: for w in words:
if w.lower()=='catholic' or w.lower()=='mother':
print(w,end=' ')
f.close() count+=1
print('Count of Catholic,mother is',count)
Write a method/function COUNTLINES_ET() in Write a method/function SHOW_TODO() in
python to read lines from a text file REPORT.TXT, python to read contents from a text file ABC.TXT
and COUNT those lines which are starting either and display those lines which have occurrence of
with ‘E’ and starting with ‘T’ respectively. And the word ‘‘TO’’ or ‘‘DO’’.
display the Total count separately. def SHOW_TODO():
def COUNTLINES_ET(): f=open(“ABC.TXT”)
f=open(“REPORT.TXT”) d=f.readlines()
d=f.readlines() for i in d:
le=0 if “TO” in i or “DO” in i:
lt=0 print(i)
for i in d: f.close()
if i[0]==’E:
le=le+1
elif i[0]==’T’:
lt=lt+1
print(“no of line start with”,le)
print(“no of line start with”,lt)
Write a function in Python that counts the Write a function AMCount() in Python, which
number of “Me” or “My” words present in a text should read each character of a text file
file “STORY.TXT”. STORY.TXT, should count and display the
def displayMeMy(): occurrences of alphabets A and M (including
num=0 small cases a and m too).
f=open("story.txt","r") def AMCount():
N=f.read() f=open("story.txt","r")
M=N.split() A,M=0,0
for x in M: r=f.read()
if x=="Me" or x== "My": for x in r:
print(x) if x[0]=="A" or x[0]=="a" :
num=num+1 A=A+1
print("Count of Me/My in file:",num) elif x[0]=="M" or x[0]=="m":
f.close() M=M+1
print("A or a: ",A)
f.close()

Write a function in python that displays the Write a function countmy() in Python to read file
number of lines starting with ‘H’ in the file Data.txt and count the number of times “my”
“para.txt”. occur in file.
def countH(): def countmy():
f=open("para.txt","r") f=open(“Data.txt”,”r”)
lines=0 count=0
l=f.readlines() x=f.read()
for i in l: word=x.split()
if i[0]='H': for i in word:
lines+=1 if i ==”my” :
print("NO of lines are:",lines) count=count+1
f.close() print(“my occurs “, count, “times”)

Write a Python program to find the number of Write a Python program to count the word “if “ in
lines in a text file ‘abc.txt’. a text file abc.txt’.
f=open("abc.txt","r") file=open("abc.txt","r")
d=f.readlines() c=0
count=len(d) line = file.read()
print(count) word = line.split()
f.close() for w in word:
if w=='if':
print( w)
c=c+1
print(c)
file.close()
Write a method in python to read lines from a Write a method/function ISTOUPCOUNT() in
text file DIARY.TXT and display those lines which python to read contents from a text file
start with the alphabets P. WRITER.TXT, to count and display the occurrence
def countp(): of the word ‘‘IS’’ or ‘‘TO’’ or ‘‘UP’’
f=open("diary.txt","r")
lines=0 def ISTOUPCOUNT():
l=f.readlines() c=0
for i in l: file=open('sample.txt','r')
if i[0]=='P': line = file.read()
lines+=1 word = line.split()
print("No of lines are:",lines) cnt=0
for w in word:
if w=='TO' or w=='UP' or w=='IS':
cnt+=1
print(cnt)
file.close()
Write a code in Python that counts the number of Write a function VowelCount() in Python, which
“The” or “This” words present in a text file should read each character of a text file
“MY_TEXT_FILE.TXT”. MY_TEXT_FILE.TXT, should count and display the
c=0 occurrence of alphabets vowels.
f=open('MY_TEXT_FILE.TXT', 'r') :
d=f.read() def VowelCount():
w=d.split() count_a=count_e=count_i=count_o=count_u=0
for i in w: f= open('MY_TEXT_FILE.TXT', 'r')
if i.upper()== 'THE' or i.upper()== 'THIS' : d=f.read()
c+=1 for i in d:
print(c) if i.upper()=='A':
count_a+=1
elif letter.upper()=='E':
count_e+=1
elif letter.upper()=='I':
count_i+=1
elif letter.upper()=='O':
count_o+=1
elif letter.upper()=='U':
count_u+=1
print("A or a:", count_a)
print("E or e:", count_e)
print("I or i:", count_i)
print("O or o:", count_o)
print("U or u:", count_u)
Write a function filter(oldfile, newfile) that copies
all the lines of a text file “source.txt” onto
“target.txt” except those lines which starts with
“@” sign.

def filter(oldfile, newfile):


f1 = open("oldfile","r")
f2 = open(“newfile”,”w”)
while True:
text= f1.readline()
if len(text) ==0:
break
if text[0] == ‘@’:
continue
f2.write(text)
f1.close()
f2.close()

You might also like