Skip to content
Computer Science · Class 12

Active learning ideas

Text File Handling: Read and Write Modes

Active learning helps students grasp file handling concretely because it lets them see exactly what happens when they open, read, or write to a file. Instead of just reading about modes, students will experience how 'w' overwrites data and 'a' preserves it, making the concept memorable and practical.

CBSE Learning OutcomesCBSE: Computational Thinking and Programming - File Handling - Class 12
15–30 minPairs → Whole Class4 activities

Activity 01

Peer Teaching20 min · Pairs

File Mode Challenge

Students create a text file with student data and write a Python script to open it in different modes: read to display content, write to overwrite, and append to add new entries. They note changes after each operation. This reinforces mode implications.

Explain the different modes for opening a file in Python and their implications.

Facilitation TipFor the File Mode Challenge, give students a single file and ask them to write three different snippets, each using a different mode, and observe how the file changes.

What to look forPresent students with three code snippets, each opening a file in 'r', 'w', and 'a' mode respectively. Ask them to predict the content of the file after each snippet executes and explain their reasoning.

UnderstandApplyAnalyzeCreateSelf-ManagementRelationship Skills
Generate Complete Lesson

Activity 02

Peer Teaching15 min · Pairs

Line Reader Script

Pairs develop a programme that reads specific lines from a story file using readline() or line numbers. They handle errors and print targeted content. This practises precise reading.

Construct a Python script to read specific lines from a text file.

Facilitation TipWhen students work on the Line Reader Script, ask them to predict the output before running the code, then compare their predictions with the actual result.

What to look forProvide students with a small text file. Ask them to write a Python command to read the third line of the file and another command to append the text 'End of file.' to it. They should also write one sentence on why `file.close()` is important.

UnderstandApplyAnalyzeCreateSelf-ManagementRelationship Skills
Generate Complete Lesson

Activity 03

Peer Teaching25 min · Individual

Data Logger

Individuals build a script to write user inputs to a log file in append mode, then read and display the log. They ensure proper closing. This simulates real data logging.

Evaluate the importance of closing files after operations are complete.

Facilitation TipDuring the Data Logger activity, remind students to test their script with multiple entries to see how 'a' mode behaves differently from 'w'.

What to look forPose the scenario: 'You have a log file that needs to record every error that occurs. Which file mode would you use and why? What could happen if you forget to close the file after writing?' Facilitate a class discussion on their answers.

UnderstandApplyAnalyzeCreateSelf-ManagementRelationship Skills
Generate Complete Lesson

Activity 04

Peer Teaching30 min · Small Groups

Mode Comparison

Small groups compare outputs of scripts using 'w' versus 'a' on the same file, discussing overwrite risks. They present findings to the class.

Explain the different modes for opening a file in Python and their implications.

Facilitation TipFor Mode Comparison, provide a template with placeholders for each mode and have students fill in the correct parameters and expected outcomes.

What to look forPresent students with three code snippets, each opening a file in 'r', 'w', and 'a' mode respectively. Ask them to predict the content of the file after each snippet executes and explain their reasoning.

UnderstandApplyAnalyzeCreateSelf-ManagementRelationship Skills
Generate Complete Lesson

A few notes on teaching this unit

Teachers should start by demonstrating how file modes behave in real time, using the same file to show overwriting, appending, and reading. Avoid abstract explanations; instead, let students experiment with small files they can visually inspect. Encourage them to always use the `with` statement for automatic file closing, as it reduces errors and models good practice. Research shows that hands-on file operations, even with tiny datasets, build stronger understanding than theoretical discussions.

By the end of these activities, students will confidently choose the right file mode for different tasks. They will correctly use read(), write(), and close() functions, and explain why forgetting to close a file can cause problems in real-world applications.


Watch Out for These Misconceptions

  • During File Mode Challenge, watch for students who assume 'r' can also write to a file without errors.

    Use the File Mode Challenge to show that 'r' raises an error if you try to write. Ask students to intentionally break their code by adding a write() call in 'r' mode, then observe the error message.

  • During Data Logger, watch for students who forget to close the file after writing, thinking it doesn’t matter for small logs.

    Ask students to run their Data Logger script multiple times and check if the file updates correctly. If data is missing, guide them to compare scripts that use `close()` versus those that don’t.

  • During Line Reader Script, watch for students who assume read() reads line by line by default.

    Have students test read() on a multi-line file and observe that it returns the entire content. Then, ask them to modify the script to use readline() to read only one line at a time.


Methods used in this brief