


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This document contains a complete collection of Python text file handling exam-based questions with solved programs and examples. It covers important topics such as word counting, line counting, character occurrence, filtering text files, file reading operations, and user-defined functions. Designed for students preparing for school practicals and examinations, this guide helps strengthen Python file handling concepts through practice-oriented questions and solutions. Suitable for beginners as well as exam preparation and quick revision.
Typology: Assignments
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Text File – Exam based questions
Write a function in python to count the number lines in a text file ‘Country.txt’ which is starting with an alphabet ‘W’ or ‘H’. def count_W_H(): f = open (“Country.txt”, “r”) W,H = 0, r = f.read() for x in r: if x[0] == “W” or x[0] == “w”: W=W+ elif x[0] == “H” or x[0] == “h”: H=H+ f.close() print (“W or w :”, W) print (“H or h :”, H)
Write a user defined function countwords() to display the total number of words present in the file from a text file “Quotes.Txt”.
def countwords(): s = open("Quotes.txt","r") f = s.read() z = f.split () count = 0 for i in z: count = count + 1 print ("Total number of words:", count)
Write a user defined function countwords() to display the total number of words present in the file from a text file “Quotes.Txt”. def countwords(): s = open("Quotes.txt","r") f = s.read() z = f.split () count = 0 for i in z: count = count + 1 print ("Total number of words:", count)
Write a function COUNT_AND( ) in Python to read the text file “STORY.TXT” and count the number of times “AND” occurs in the file. (include AND/and/And in the counting) def COUNT_AND( ): count= file=open(‘STORY.TXT','r') line = file.read() word = line.split() for w in word: if w ==’AND’: count=count+ print(count) file.close()
Write a function DISPLAYWORDS( ) in python to display the count of words starting with “t” or “T” in a text file ‘STORY.TXT’. def COUNT_AND( ): count= file=open(‘STORY.TXT','r') line = file.read() word = line.split() for w in word: if w[0] ==’t’ or w[0]==’T’: count=count+ print(count) file.close()
Write a function that counts and display the number of 5 letter words in a text file “Sample.txt def count_words( ): c = 0 f = open("Sample.txt") line = f.read() word = line.split() for w in word: if len(w) == 5: c += 1 print(c)
Write a function that counts and display the number of 5 letter words in a text file “Sample.txt def count_words( ): c = 0 f = open("Sample.txt")
Write a function that counts and display the number of 5 letter words in a text file “Sample.txt def count_words( ): c = 0 f = open("Sample.txt")
line = f.read() word = line.split() for w in word: if len(w) == 5: c += 1 print(c)
line = f.read() word = line.split() for w in word: if len(w) == 5: c += 1 print(c)
Write a function that counts and display the number of 5 letter words in a text file “Sample.txt def count_words( ): c = 0 f = open("Sample.txt") line = f.read() word = line.split() for w in word: if len(w) == 5: c += 1 print(c) f.close()
Write a function to display those lines which start with the letter “G” from the text file “MyNotes.txt” def count_lines( ): c = 0 f = open("MyNotes.txt") line = f.readlines() for w in line: if w[0] == 'G': print(w) f.close()
Write a function in python to read lines from file “POEM.txt” and display all those words, which has two characters in it. def TwoCharWord(): f = open('poem.txt') count = 0 for line in f: words = line.split() for w in words: if len(w)==2: print(w,end=' ') f.close()
Write a function COUNT() in Python to read contents from file “REPEATED.TXT”, to count and display the occurrence of the word “Catholic” or “mother”. def COUNT(): f = open('REPEATED.txt') count = 0 for line in f: words = line.split() for w in words: if w.lower()=='catholic' or w.lower()=='mother': count+= print('Count of Catholic,mother is',count)
Write a method/function COUNTLINES_ET() in python to read lines from a text file REPORT.TXT, and COUNT those lines which are starting either with ‘E’ and starting with ‘T’ respectively. And display the Total count separately. def COUNTLINES_ET(): f=open(“REPORT.TXT”) d=f.readlines() le= lt= for i in d: if i[0]==’E: le=le+ elif i[0]==’T’: lt=lt+ print(“no of line start with”,le) print(“no of line start with”,lt)
Write a method/function SHOW_TODO() in python to read contents from a text file ABC.TXT and display those lines which have occurrence of the word ‘‘TO’’ or ‘‘DO’’. def SHOW_TODO(): f=open(“ABC.TXT”) d=f.readlines() for i in d: if “TO” in i or “DO” in i: print(i) f.close()
lines+= print("No of lines are:",lines)
word = line.split() cnt= for w in word: if w=='TO' or w=='UP' or w=='IS': cnt+= print(cnt) file.close() Write a code in Python that counts the number of “The” or “This” words present in a text file “MY_TEXT_FILE.TXT”. c = 0 f=open('MY_TEXT_FILE.TXT', 'r') : d=f.read() w=d.split() for i in w: if i.upper()== 'THE' or i.upper()== 'THIS' : c+= print(c)
Write a function VowelCount() in Python, which should read each character of a text file MY_TEXT_FILE.TXT, should count and display the occurrence of alphabets vowels.
def VowelCount(): count_a=count_e=count_i=count_o=count_u= f= open('MY_TEXT_FILE.TXT', 'r') d=f.read() for i in d: if i.upper()=='A': count_a+= elif letter.upper()=='E': count_e+= elif letter.upper()=='I': count_i+= elif letter.upper()=='O': count_o+= elif letter.upper()=='U': count_u+= 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()