File basics, paths, modes, seek and tell Theory — Lecture 23
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: "File basics, paths, modes, seek and tell" Lecture number 23 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 "File basics, paths, modes, seek and tell" 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: File basics, paths, modes, seek and tell Theory Subtopics to cover today: - File basics, paths, modes, seek and tell Student level: Class XII, CBSE Board, average to above-average students preparing for board exams =========================================== SECTION 2: TEACHER'S REFERENCE NOTES =========================================== File basics, paths, modes, seek and tell Theory Concept ID: U1_FILE_BASICS_MODES. Primary Sub-subtopic: File basics, paths, modes, seek and tell. 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: File basics paths modes seek and tell, File basics paths modes seek and tell, Binary file operations with pickle, File basics paths modes seek and tell, CSV file read write and search, File basics paths modes seek and tell, Text file reading writing searching and counting, Text file reading writing searching and counting | Year | Questions | Marks | |------|-----------|-------| | 2021 | 7 | 7 | | 2022 | 2 | 2 | | 2023 | 9 | 21 | | 2024 | 3 | 9 | | 2025 | 4 | 11 | | 2026 | 1 | 1 | | **Total** | **26** | **51** | =========================================== SECTION 4: EXAM ANALYSIS INPUT — DO NOT PRINT THESE IN THEORY NOTES (Scope: "File basics, paths, modes, seek and tell" only — 26 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 --- 2021 Board Exam (7 questions | 7 marks) --- Q1. [MCQ] [1M] [Easy] Section-A Which of the following option is not correct? a) if we try to read a text file that does not exist, an error occurs. b) if we try to read a text file that does not exist, the file gets created. c) if we try to write on a text file that does not exist, no error occurs. d) if we try to write on a text file that does not exist, the file gets created. Q2. [MCQ] [1M] [Easy] Section-A A text file student.txt is stored in the storage device. Identify the correct option out of the following options to open the file in read mode. i. myfile = open('student.txt','rb') ii. myfile = open('student.txt','w') iii. myfile = open('student.txt','r') iv. myfile = open('student.txt') a) only i b) both i and iv c) both iii and iv d) both i and iii Q3. [MCQ] [1M] [Easy] Section-A What is the significance of the tell() method? a) tells the path of file b) tells the current position of the file pointer within the file c) tells the end position within the file d) checks the existence of a file at the desired location Q4. [MCQ] [1M] [Easy] Section-A Syntax of seek function in Python is myfile.seek(offset, reference_point) where myfile is the file object. What is the default value of reference_point? a) 0 b) 1 c) 2 d) 3 Q5. [MCQ] [1M] [Easy] Section-A Syntax for opening Student.csv file in write mode is myfile = open("Student.csv","w",newline=''). What is the importance of newline=''? a) A newline gets added to the file b) Empty string gets appended to the first line. c) Empty string gets appended to all lines. d) EOL translation is suppressed Q6. [MCQ] [1M] [Easy] Section-A Which of the following statement opens a binary file record.bin in write mode and writes data from a list lst1 = [1,2,3,4] on the binary file? a) with open('record.bin','wb') as myfile: pickle.dump(lst1,myfile) b) with open('record.bin','wb') as myfile: pickle.dump(myfile,lst1) c) with open('record.bin','wb+') as myfile: pickle.dump(myfile,lst1) d) with open('record.bin','ab') as myfile: pickle.dump(myfile,lst1) Q7. [MCQ] [1M] [Easy] Section-B Consider the following directory structure. (Root: School → Subdirectories: Academics [contains Syllabus.jpg], Examination [contains Result.png], Sports [contains Achievement.jpg]) Suppose root directory (School) and present working directory are the same. What will be the absolute path of the file Syllabus.jpg? a) School/syllabus.jpg b) School/Academics/syllabus.jpg c) School/Academics/../syllabus.jpg d) School/Examination/syllabus.jpg --- 2022 Board Exam (2 questions | 2 marks) --- Q1. [MCQ] [1M] [Easy] Section-A Which of the following mode in file opening statement results or generates an error if the file does not exist? a) a+ b) r+ c) w+ d) None of the above Q2. [MCQ] [1M] [Easy] Section-A The correct syntax of seek() is: a) file_object.seek(offset [, reference_point]) b) seek(offset [, reference_point]) c) seek(offset, file_object) d) seek.file_object(offset) --- 2023 Board Exam (9 questions | 21 marks) --- Q1. [MCQ] [1M] [Easy] Section-A Which of the following mode keeps the file offset position at the end of the file ? (a) r+ (b) x (c) w (d) a Q2. [MCQ] [1M] [Easy] Section-A The syntax of seek( ) is file object.seek(offset[,reference point] ) What is the default value of reference point ? (a) 0 (b) 1 (c) 2 (d) 3 Q3. [MCQ] [1M] [Easy] Section-A Which of the following modes in Python creates a new file, if file does not exist and overwrites the content, if the file exists ? (a) r+ (b) r (c) w (d) a Q4. [MCQ] [1M] [Easy] Section-A The correct syntax of tell() is: (a) tell.file object () (b) file object.tell() (c) tell.file object (1) (d) file object.tell1(1) Q5. [MCQ] [1M] [Easy] Section-A Which of the following functions changes the position of file pointer and returns its new position? a) flush() b) tell() c) seek() d) offset() Q6. [Programming] [3M] [Medium] Section-C (a) Write a function in Python that displays the book names having 'Y' or 'y' in their name from a text file "Bookname.txt". Example : If the file 'Bookname.txt' contains the names of following books : One Hundred Years of Solitude The Diary of a Young Girl On the Road After execution, the output will be : One Hundred Years of Solitude The Diary of a Young Girl Q7. [Programming] [3M] [Medium] Section-C Write a function in Python to read a text file, Alpha.txt and displays those lines which begin with the word 'You'. Q8. [Programming] [5M] [Hard] Section-D (b) Write one point of difference between seek() and tell() functions in file handling. Write a program in Python that defines and calls the following user defined functions : (i) Add_Device() : The function accepts and adds records of the peripheral devices to a csv file 'peripheral.csv'. Each record consists of a list with field elements as P_id, P_name and Price to store peripheral device ID, device name, and price respectively. (ii) Count_Device() : To count and display number of peripheral devices, whose price is less than or equal to 1000. Q9. [Programming] [5M] [Hard] Section-E (i) Differentiate between r+ and w+ file modes in Python. (ii) Consider a file, SPORT.DAT, containing records of the following structure: [SportName, TeamName, No_Players] Write a function, copyData(), that reads contents from the file SPORT.DAT and copies the records with Sport name as "Basket Ball" to the file named BASKET.DAT. The function should return the total number of records copied to the file BASKET.DAT. (ii) A Binary file, CINEMA.DAT has the following structure: {MNO:[MNAME, MTYPE]} Where MNO – Movie Number, MNAME – Movie Name, MTYPE is Movie Type. Write a user defined function, findType(mtype), that accepts mtype as parameter and displays all the records from the binary file CINEMA.DAT, that have the value of Movie Type as mtype. --- 2024 Board Exam (3 questions | 9 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 Write the user defined function Modify() to read a text file "data.txt" and replace all the occurrences of "COmputer" with "Computer" and display all the lines on screen. Q3. [Programming] [5M] [Hard] Section-E (a) (i) What is the main purpose of seek() and tell() method ? (ii) Consider a binary file, Cinema.dat containing information in the following structure : [Mno, Mname, Mtype] Write a function, search_copy(), that reads the content from the file Cinema.dat and copies all the details of the "Comedy" movie type to file named movie.dat. (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 (4 questions | 11 marks) --- Q1. [Assertion-Reason] [1M] [Easy] Section-A Assertion (A): For a binary file opened using 'rb' mode, the pickle.dump() method will display an error. Reason (R): The pickle.dump() method is used to read from a binary file. (A) Both Assertion (A) and Reason (R) are true and Reason (R) is the correct explanation for Assertion (A). (B) Both Assertion (A) and Reason (R) are true and Reason (R) is not the correct explanation for Assertion (A). (C) Assertion (A) is true but Reason (R) is false. (D) Assertion (A) is false but Reason (R) is true. Q2. [Short Answer] [3M] [Medium] Section-C (a) Write a Python function that displays all the lines ending with a dot (.) from a text file, "Colors.txt". For example, if the file "Colors.txt" contains : White is a mix of seven colors. What are these seven colors ? VIBGYOR – violet, indigo, blue, green, orange and red. When we mix all these colors we just get one light which is the WHITE light. Then the output should be : White is a mix of seven colors. VIBGYOR – violet, indigo, blue, green, orange and red. which is the WHITE light. (Hint : You will have to ignore trailing white spaces to check the last character) Q3. [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". Q4. [Programming] [4M] [Medium] Section-E Write a program in Python to create a text file MARKS.TXT and write the names of all those students and their total marks obtained in Physics, Chemistry and Maths whose total marks are greater than 220. --- 2026 Board Exam (1 question | 1 marks) --- Q1. [MCQ] [1M] [Easy] Section-A Consider the statement given below : f1 = open("pqr.dat","_______") Which of the following is the correct file mode to open the file in read only mode ? (A) a (B) rb (C) r+ (D) rb+ =========================================== =========================================== 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: File basics paths modes seek and tell Pattern: MCQ=12, Programming=4, Short Answer=1 | Marks: 1M=12, 3M=4, 4M=1 | Total: 17 questions [MCQ] [1M] [Easy] × 8 → Core concept of File basics paths modes seek and tell [MCQ] [1M] [Easy] × 4 → Exception can occur even when syntax is correct [Programming] [3M] [Medium] → Output/result interpretation for the current concept [Programming] [3M] [Medium] × 2 → Core concept of File basics paths modes seek and tell [Short Answer] [3M] [Medium] → Output/result interpretation for the current concept [Programming] [4M] [Medium] → Core concept of File basics paths modes seek and tell ### Concept: File basics paths modes seek and tell, Binary file operations with pickle Pattern: Assertion-Reason=1, MCQ=2, Programming=2 | Marks: 1M=3, 5M=2 | Total: 5 questions [MCQ] [1M] [Easy] × 2 → Core concept of File basics paths modes seek and tell, Binary file operations with pickle [Programming] [5M] [Hard] × 2 → Core concept of File basics paths modes seek and tell, Binary file operations with pickle [Assertion-Reason] [1M] [Easy] → Core concept of File basics paths modes seek and tell, Binary file operations with pickle ### Concept: File basics paths modes seek and tell, CSV file read write and search Pattern: MCQ=1, Programming=1 | Marks: 1M=1, 5M=1 | Total: 2 questions [MCQ] [1M] [Easy] → Exception can occur even when syntax is correct [Programming] [5M] [Hard] → Core concept of File basics paths modes seek and tell, CSV file read write and search ### 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: Programming=1 | Marks: 3M=1 | Total: 1 questions [Programming] [3M] [Medium] → Core concept of Text file reading writing searching and counting =========================================== =========================================== IMPORTANCE ANALYSIS (allocate teaching time by this ranking) =========================================== | Rank | Concept | Score | Times Tested | Total Marks | Recent Years | Priority | |------|---------|-------|-------------|-------------|--------------|----------| | 1 | File basics paths modes seek and tell | 82 | 17 | 28M | 2025, 2024, 2023 | CRITICAL | | 2 | File basics paths modes seek and tell, Binary file operations with pickle | 45 | 5 | 13M | 2026, 2025, 2024 | CRITICAL | | 3 | File basics paths modes seek and tell, CSV file read write and search | 16 | 2 | 6M | 2023, 2021 | HIGH | | 4 | Text file reading writing searching and counting | 12 | 1 | 3M | 2025 | HIGH | | 5 | 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 "File basics, paths, modes, seek and tell". Self-generate 4–7 traps from: - actual question patterns for File basics paths modes seek and tell, File basics paths modes seek and tell, Binary file operations with pickle, File basics paths modes seek and tell, CSV file read write and search, 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