Board Q&A · Class 12 Computer Science

File Handling in Python (Text Files): Class 12 CBSE Previous-Year Questions with Answers

CBSE Question Papers →

13 questions from CBSE board, sample and compartment papers (2024–2027), 1 to 3 marks, each with a model answer. Questions on opening and closing text files, read(), readline() and readlines(), write() and writelines(), file modes and the file pointer (seek and tell), and functions that count or display lines and words. Programming answers open in the Python Lab.

Showing 13 of 295 questions
Group

File Handling in Python (Text File)

13 Questions
File Handling in Python (Text File)· 1 Mark· Q16· 2024 Board Paper
Consider the following Python statement: F=open('CONTENT.TXT') Which of the following is an invalid statement in Python?
  1. (A)F.seek(1,0)
  2. (B)F.seek(0,1)
  3. (C)F.seek(0,-1)
  4. (D)F.seek(0,2)
File Handling in Python (Text File)· 3 Marks· Q28· 2024 Board Paper Python
(A) Write a user defined function in Python named showInLines() which reads contents of a text file named STORY.TXT and displays every sentence in a separate line. Assume that a sentence ends with a full stop (.), a question mark (?), or an exclamation mark (!). For example, if the content of file STORY.TXT is as follows: Our parents told us that we must eat vegetables to be healthy. And it turns out, our parents were right! So, what else did our parents tell? Then the function should display the file's content as follows: Our parents told us that we must eat vegetables to be healthy. And it turns out, our parents were right! So, what else did our parents tell? OR (B) Write a function, c_words() in Python that separately counts and displays the number of uppercase and lowercase alphabets in a text file, Words.txt.
File Handling in Python (Text File)· 3 Marks· Q29· 2025 Board Paper Python
(a) Write a Python function that displays all the lines containing the word 'vote' from a text file "Elections.txt". For example, if the file contains: In an election many people vote to choose their representative. The candidate getting the maximum share of votes stands elected. Normally, one person has to vote once. The process of voting may vary with time and region. Then the output should be: In an election many people vote to choose their representative. Normally, one person has to vote once.
File Handling in Python (Text File)· 3 Marks· Q29· 2025 Board Paper Python
(b) Write a Python function that displays all the words starting and ending with a vowel from a text file "Report.txt". The consecutive words should be separated by a space in the output. For example, if the file contains: Once there was a wise man in a village. He was an awesome story-teller. He was able to keep people anchored while listening to him. Then the output should be: Once a a awesome able
File Handling in Python (Text File)· 3 Marks· Q29· 2026 Board Paper Python
(a) Write a Python function that counts and returns the number of digits appearing in the text file "Space.txt". OR (b) Write a Python function that displays the words in which the lowercase letter 'e' appears at least twice in "Space.txt".
File Handling in Python (Text File)· 1 Mark· Q10· 2025 Compartment Paper Python
A text file song.txt contains the following contents in it: Life goes on as it never ends What will be the output of the following code snippet? f1 = open("song.txt","r") s1 = f1.read(5) s2 = f1.readline(4) s3 = f1.read(3) print(s1, s3, sep="#")
  1. (A)goes# on
  2. (B)Life #goes# on
  3. (C)Life # on
  4. (D)Error
File Handling in Python (Text File)· 3 Marks· Q29· 2025 Compartment Paper Python
(a) Write a Python function that displays all lines ending with a dot (.) from a text file "Colors.txt". OR (b) Write a function to display the line which has the maximum number of vowels from a text file, "Novel.txt".
File Handling in Python (Text File)· 1 Mark· Q16· 2024 Sample Paper
Which of the following functions changes the position of file pointer and returns its new position?
  1. (A)a. flush()
  2. (B)b. tell()
  3. (C)c. seek()
  4. (D)d. offset()
File Handling in Python (Text File)· 3 Marks· Q28· 2024 Sample Paper
Write a function in Python to read a text file, Alpha.txt and displays those lines which begin with the word ‘You’. OR Write a function, vowelCount() in Python that counts and displays the number of vowels in the text file named Poem.txt.
File Handling in Python (Text File)· 1 Mark· Q10· 2025 Sample Paper
Write the missing statement to complete the following code: file = open("example.txt", "r") data = file.read(100) ____________________ #Move the file pointer to the beginning of the file next_data = file.read(50) file.close()
File Handling in Python (Text File)· 3 Marks· Q29· 2025 Sample Paper
A) Write a Python function that displays all the words containing @cmail from a text file "Emails.txt". OR B) Write a Python function that finds and displays all the words longer than 5 characters from a text file "Words.txt".
File Handling in Python (Text File)· 3 Marks· Q29· 2026 Sample Paper Python
A. Write a Python function that displays the number of times the word "Python" appears in a text file named "Prog.txt". OR B. Write and call a Python function to read lines from a text file STORIES.TXT and display those lines which doesn't start with a vowel (A, E, I, O, U) irrespective of their case.
File Handling in Python (Text File)· 3 Marks· Q29· 2027 Sample Paper
A. Write and call a Python function to read "History.txt" and display all lines that contain a four-digit year (e.g., 1999, 2024 etc.) OR B. Write a Python function that displays the number of times the word "Python" appears in a text file named "Prog.txt".