Text file reading writing searching and counting Theory part 2 — Lecture 27
CBSE | XII CS | Computational Thinking and Programming – 2 | 35 min
You are an expert CBSE XII CS Computer Science teacher, examiner, and study material creator. =========================================== SCOPE — READ BEFORE GENERATING ANYTHING =========================================== Today's lecture covers ONE topic only: "Text file reading writing searching and counting" Lecture number 27 of 91 | Duration: 35 minutes | Board: CBSE Chapter: Computational Thinking and Programming – 2 HARD RULE: Every piece of content you generate — notes, examples, questions, tips — must be directly relevant to "Text file reading writing searching and counting" only. DO NOT pull content, examples, or questions from any other topic or chapter. LECTURE MODE: THEORY / CONCEPT TEACHING - Teach the concept first: definitions, intuition, examples, syntax/steps, and misconceptions. - Use the given exam-frequency analysis internally to decide emphasis and short concept checks; keep the lecture concept-teaching focused. - Keep content tightly scoped to today's concept list. =========================================== SECTION 1: LECTURE INFORMATION =========================================== Class: XII CS | Subject: Computer Science | Board: CBSE Topic: Text file reading writing searching and counting Theory part 2 Subtopics to cover today: - Text file reading writing searching and counting Student level: Class XII, CBSE Board, average to above-average students preparing for board exams =========================================== SECTION 2: TEACHER'S REFERENCE NOTES =========================================== Text file reading writing searching and counting Theory part 2 Concept ID: U1_TEXT_FILE_OPERATIONS. Primary Sub-subtopic: Text file reading writing searching and counting. Question-bank grouping: File Handling. Use this lecture for theory, examples, misconceptions, and short concept checks mapped to this concept ID. Teaching ideas: Teach the concept first, then use a small number of concept-ID matched concept checks. =========================================== SECTION 3: EXAM FREQUENCY DATA (Year-wise) =========================================== Teaching priority: HIGH High-yield concepts: Binary file operations with pickle, CSV file read write and search, Text file reading writing searching and counting, File basics paths modes seek and tell, Text file reading writing searching and counting, Text file reading writing searching and counting | Year | Questions | Marks | |------|-----------|-------| | 2023 | 1 | 5 | | 2024 | 9 | 28 | | 2025 | 5 | 13 | | 2026 | 2 | 6 | | **Total** | **17** | **52** | =========================================== SECTION 4: EXAM ANALYSIS INPUT — DO NOT PRINT THESE IN THEORY NOTES (Scope: "Text file reading writing searching and counting" only — 17 questions from board papers) =========================================== THEORY LECTURE RULE: use these PYQs only as private analysis data — this lecture is PURELY THEORY. - Cover every concept and trap revealed by these PYQs in the study notes - Do NOT print, quote, reproduce, paraphrase, or label any actual PYQ in the final HTML - Self-generate your own tricky/important questions and examples about the concept itself only — do not call them "PYQ-pattern", "PYQ-style", "board-style", or reference PYQs/previous years at all - In theory lectures, generated questions must be MCQ or Short Answer only - Never use the word "PYQ" or phrases like "previous year question" anywhere in the visible HTML --- 2023 Board Exam (1 question | 5 marks) --- Q1. [Programming] [5M] [Hard] Section-D (a) Write one difference between CSV and text files. Write a program in Python that defines and calls the following user defined functions : (i) COURIER_ADD(): It takes the values from the user and adds the details to a csv file 'courier.csv'. Each record consists of a list with field elements as cid, s_name, Source, destination to store Courier ID, Sender name, Source and destination address respectively. (ii) COURIER_SEARCH() : Takes the destination as the input and displays all the courier records going to that destination. (ii) Search_Book(): Takes publisher name as input and counts and displays number of books published by them. --- 2024 Board Exam (9 questions | 28 marks) --- Q1. [MCQ] [1M] [Easy] Section-A Which of the following file opening mode is used to open a file in append and read mode ? (A) r+ (B) w+ (C) a+ (D) x+ Q2. [Programming] [3M] [Medium] Section-C (a) Write the user defined function count_words() in Python to count the number of words in a text file "Story.txt". Q3. [Programming] [3M] [Medium] Section-C (b) Write the user defined function display() in Python to read the content of a text file "Story.txt" and display those words which are less than 4 characters. Q4. [Programming] [3M] [Medium] Section-C (a) Write a method/function COUNTWORDS() in Python to read contents from a text file DECODE.TXT, to count and return the occurrence of those words, which are having 5 or more characters. Q5. [Programming] [3M] [Medium] Section-C (b) Write a method/function COUNTLINES() in Python to read lines from a text file CONTENT.TXT, and display those lines, which have @ anywhere in the line. For example : If the content of the file is : Had an amazing time at the concert last night with @MusicLoversCrew. Excited to announce the launch of our new website! G20 @ India The method/function should display Had an amazing time at the concert last night with @MusicLoversCrew G20 @ India Q6. [Short Answer] [3M] [Medium] Section-C A) Write a Python function that displays all the words containing @cmail from a text file "Emails.txt". Q7. [Short Answer] [3M] [Medium] Section-C B) Write a Python function that finds and displays all the words longer than 5 characters from a text file "Words.txt". Q8. [Programming] [4M] [Medium] Section-E Write a program in Python to read the content of a text file Story.txt and count and display the number of lines that start with the letter 'A'. Q9. [Programming] [5M] [Hard] Section-E (b) (i) Give one difference between write() and writeline() function in text file. (ii) A Binary file, "Items.dat" has the following structure : [Icode, Description, Price] Where Icode – Item code Description – Detail of item Price – Price of item Write a function Add_data(), that takes Icode, Description and Price from the user and writes the information in the binary file "Items.dat". --- 2025 Board Exam (5 questions | 13 marks) --- Q1. [MCQ] [1M] [Easy] Section-A 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="#") (A) goes# on (B) Life #goes# on (C) Life # on (D) Error Q2. [Programming] [3M] [Medium] Section-C Write a user defined function in Python named COUNT() which counts and displays the number of lines which do not start with the vowels 'A' or 'E' in the file, "Paragraph.txt". Q3. [Programming] [3M] [Medium] Section-C (b) Write a function in Python to display the line which has the maximum number of vowels from a text file, "Novel.txt" Q4. [Programming] [3M] [Medium] Section-C A. Write a Python function that displays the number of times the word "Python" appears in a text file named "Prog.txt". Q5. [Programming] [3M] [Medium] Section-C 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. --- 2026 Board Exam (2 questions | 6 marks) --- Q1. [Short Answer] [3M] [Medium] Section-C (a) Write a Python function that counts and returns the number of digits appearing in the text file "Space.txt". For example, if the file contains : Space exploration has unlocked incredible advancements in technology and science. Since the first moon landing in 1969, space agencies have sent probes to Mars, Jupiter and beyond. The ISS, orbiting Earth at about 400 km, serves as a hub for research. With missions planned for 2030, humanity's cosmic journey continues! Then the function should return 11. Q2. [Short Answer] [3M] [Medium] Section-C (b) Write a Python function that displays the words in which the lowercase letter 'e' appears at least twice in the text file 'Space.txt'. For example, if the file contains : Space exploration has unlocked incredible advancements in technology and science. Since the first moon landing in 1969, space agencies have sent probes to Mars, Jupiter and beyond. The ISS, orbiting Earth at about 400 km, serves as a hub for research. With missions planned for 2030, humanity's cosmic journey continues! Then the function should display : incredible advancements science. agencies serves research. =========================================== =========================================== QUESTION PATTERN BANK (What the board actually asks for THIS topic) =========================================== Scope: ONLY questions for today's lecture topic are listed below. DO NOT import questions from other topics or chapters. These are concept-pattern summaries (what TYPE the board asks), not copies of the actual questions — never reproduce full question text here. ### Concept: Binary file operations with pickle Pattern: Programming=1 | Marks: 5M=1 | Total: 1 questions [Programming] [5M] [Hard] → Core concept of Binary file operations with pickle ### Concept: CSV file read write and search, Text file reading writing searching and counting Pattern: Programming=1 | Marks: 5M=1 | Total: 1 questions [Programming] [5M] [Hard] → Core concept of CSV file read write and search, Text file reading writing searching and counting ### Concept: File basics paths modes seek and tell, Text file reading writing searching and counting Pattern: MCQ=1 | Marks: 1M=1 | Total: 1 questions [MCQ] [1M] [Easy] → Core concept of File basics paths modes seek and tell, Text file reading writing searching and counting ### Concept: Text file reading writing searching and counting Pattern: MCQ=1, Programming=9, Short Answer=4 | Marks: 1M=1, 3M=12, 4M=1 | Total: 14 questions [Programming] [3M] [Medium] × 8 → Core concept of Text file reading writing searching and counting [Short Answer] [3M] [Medium] × 4 → Core concept of Text file reading writing searching and counting [Programming] [4M] [Medium] → Core concept of Text file reading writing searching and counting [MCQ] [1M] [Easy] → Output/result interpretation for the current concept =========================================== =========================================== IMPORTANCE ANALYSIS (allocate teaching time by this ranking) =========================================== | Rank | Concept | Score | Times Tested | Total Marks | Recent Years | Priority | |------|---------|-------|-------------|-------------|--------------|----------| | 1 | Text file reading writing searching and counting | 108 | 14 | 41M | 2026, 2025, 2024 | CRITICAL | | 2 | CSV file read write and search, Text file reading writing searching and counting | 13 | 1 | 5M | 2023 | HIGH | | 3 | Binary file operations with pickle | 13 | 1 | 5M | 2024 | HIGH | | 4 | File basics paths modes seek and tell, Text file reading writing searching and counting | 5 | 1 | 1M | 2024 | MEDIUM | CRITICAL concepts → full sub-section + comparison table + 2 worked examples HIGH concepts → 1 sub-section + 1 worked example MEDIUM concepts → definition + 1 quick example only =========================================== EXAMINER FINGERPRINT — TRAPS TO COVER INTERNALLY =========================================== Use the exam-frequency input only for internal analysis. Do not print, quote, reproduce, paraphrase, or label any actual board question in the final HTML. For this lecture, the generated teaching material must strongly cover these traps: No static trap list exists yet for "Text file reading writing searching and counting". Self-generate 4–7 traps from: - actual question patterns for Binary file operations with pickle, CSV file read write and search, Text file reading writing searching and counting, File basics paths modes seek and tell, Text file reading writing searching and counting, Text file reading writing searching and counting - common wrong assumptions students make about this concept - output-tracing traps - syntax-vs-runtime traps - comparison traps - order/sequence traps Important: this list should be treated as dynamic — for lectures with no static trap list, generate traps yourself from the categories above rather than leaving this section thin. =========================================== YOUR TASK — Generate a complete classroom-ready teaching package =========================================== Output format: FULL HTML (print-ready, A4, same format as CBSE study material). Use the CSS classes below. NO plain Markdown — use HTML elements only. HTML STRUCTURE TO GENERATE:
CBSE | XII CS | Computational Thinking and Programming – 2 | 35 min