Text Files Class 12 PYQs 2024–2026 with Solutions | CBSE Computer Science Skip to content
Smart Notes for Every Exam · instant PDF download after payment +91 92520 37565
Class 12 · Computer Science · Chapter 4

Text Files: Class 12 PYQs 2024–2026 with solutions

All 6 CBSE board questions asked from Text Files in 3 board papers, with the year, set and marks of each. 5 are solved free below. The Class 12 Computer Science PYQ Book: Solved Board Papers 2024-2026 (CBSE) has every solution, all chapters and 3 full board papers.

Cover of Class 12 Computer Science PYQ Book: Solved Board Papers 2024-2026 (CBSE)
Class 12 Computer Science PYQ book: every chapter + 3 board papers, all solved
₹99₹14934% off
Free sample

Text Files: board weightage

Board weightage 2024–2026

Year202420252026
Questions411
Marks833

Most asked topics

File modes, seek and tell5
Text file programs1

6 questions from 3 papers. Repeated questions are shown once with every year they were asked.

Jump to: 1 mark2 marks3 marks

1 mark: MCQ and Assertion–Reason

Q1 · CBSE 2024 · 91 · Q161 mark

F=open('CONTENT.TXT'). Which is an invalid statement? (a) F.seek(1,0) (b) F.seek(0,1) (c) F.seek(0,-1) (d) F.seek(0,2)

Solution

(c) F.seek(0,-1). The reference point can only be 0, 1 or 2; −1 is invalid (ValueError: invalid whence).

2 marks: very short answer

Q2 · CBSE 2024 · 91 · Q34(A)(i)2 marks

(A)(i) Differentiate between 'w' and 'a' file modes.

Solution

'w': opens for writing; creates the file if absent; the pointer is at the beginning, so existing contents are erased. 'a': opens for appending; creates the file if absent; the pointer is at the end, so new data is added after the existing contents.

Q3 · CBSE 2024 · 91 · Q34(B)(i)2 marks

(B)(i) What is the advantage of using the with clause while opening a data file? Give its syntax.

Solution

A file opened with the with clause is closed automatically once control comes out of the with block (even after an exception). Syntax: with open(file_name, access_mode) as file_object: e.g. with open("myfile.txt","r+") as fo: content = fo.read().

3 marks: short answer

Q4 · CBSE 2026 · 91 · Q293 marks

(a) Write a function that counts and returns the number of digits in "Space.txt" (sample returns 11). OR (b) Display the words in which lowercase 'e' appears at least twice in 'Space.txt'.

Solution

def CountDigits():
with open("Space.txt", "r") as F:
Data = F.read()
C = 0
for CH in Data:
if CH.isdigit():
C += 1
return C
Digits in 1969, 400, 2030 → 4 + 3 + 4 = 11.

def showWords():
with open("Space.txt", "r") as F:
for word in F.read().split():
if word.count('e') >= 2:
print(word, end=' ')

incredible advancements science. agencies serves research.

Q5 · CBSE 2025 · 91 · Q293 marks

(a) Write a function that displays all lines containing the word 'vote' from "Elections.txt". OR (b) Display all words starting and ending with a vowel from "Report.txt", separated by a space.

Solution

def PrintVote():
with open("Elections.txt") as F:
for Line in F:
if "vote" in Line.split(): # whole word only
print(Line, end="")

For the sample file it prints lines 1 and 3; "votes" and "voting" in lines 2 and 4 are not the word "vote", which is why split() is used instead of "vote" in Line.

def vowels():
with open("Report.txt") as F:
for W in F.read().split():
if W[0] in "aeiouAEIOU" and W[-1] in "aeiouAEIOU":
print(W, end=" ")

Once a a awesome able

Q6 · CBSE 2024 · 91 · Q283 marks

(A) Write showInLines() that reads STORY.TXT and displays every sentence in a separate line (a sentence ends with . ? or !). OR (B) Write c_words() that separately counts and displays the number of uppercase and lowercase letters in Words.txt.

Full step-by-step solution is in the PYQ book (₹99).

Get every solution for all chapters

  • Every board question of every chapter, each fully solved the way the board gives marks
  • 3 complete board papers, each followed by its solutions
  • Weightage and most-asked topics for every chapter
  • Instant PDF download, colour and black & white print copies
See real pages (free sample)

Revising the chapter first? Complete Computer Science Notes for Class 12 (CBSE 083, 2026-27) →

What you get in the pack

All chapters in this pack

  1. PYQ Ch 1 Python Revision Tour
  2. PYQ Ch 2 Functions
  3. PYQ Ch 3 Exception Handling
  4. PYQ Ch 4 Text Files
  5. PYQ Ch 5 Binary Files
  6. PYQ Ch 6 CSV Files
  7. PYQ Ch 7 Stack
  8. PYQ Ch 8 Computer Networks I: Evolution and Data Communication
  9. PYQ Ch 9 Computer Networks II: Devices, Topologies and Types
  10. PYQ Ch 10 Network Protocols and Web Services
  11. PYQ Ch 11 Database Concepts
  12. PYQ Ch 12 SQL: DDL, DML and Queries
  13. PYQ Ch 13 SQL: Aggregates, Grouping and Joins
  14. PYQ Ch 14 Interface of Python with MySQL
  15. Board Paper 2026 Set 91 with Solutions
  16. Board Paper 2025 Set 91 with Solutions
  17. Board Paper 2024 Set 91 with Solutions
← PYQ Ch 3 Exception Handling PYQ Ch 5 Binary Files →

Prices shown in Indian rupees. Show prices in: ₹ INR · US$ USD