Skip to content
Computing · Year 9

Active learning ideas

Writing to Text Files

Active learning engages students with immediate feedback loops when writing to files, turning abstract persistence concepts into tangible results they can inspect and discuss. Hands-on file creation and inspection build muscle memory for open(), write(), and close() sequences that textbooks often leave as dry theory.

National Curriculum Attainment TargetsKS3: Computing - Programming and DevelopmentKS3: Computing - Data Representation
20–45 minPairs → Whole Class4 activities

Activity 01

Project-Based Learning30 min · Pairs

Pair Challenge: High-Score Tracker

Pairs write a program that asks for a name and score, then appends it to 'highscores.txt'. Test by running multiple times and viewing the file. Discuss why 'a' mode prevents data loss.

Justify the need for data persistence in applications like a high-score tracker.

Facilitation TipDuring the Pair Challenge, circulate with a file explorer open on your device to show students where to look for their saved files after each write.

What to look forProvide students with a scenario: 'You are building a simple diary application. What file mode would you use to add a new entry each day without losing previous entries, and why?' Students write their answer on a slip of paper.

ApplyAnalyzeEvaluateCreateSelf-ManagementRelationship SkillsDecision-Making
Generate Complete Lesson

Activity 02

Project-Based Learning45 min · Small Groups

Small Group: Note-Taking App

Groups design a loop for user notes input, save to 'notes.txt' using 'w' first then 'a' for additions. Run, edit file externally, rerun to observe overwrites. Compare file states.

Design a program that allows a user to input notes and saves them to a text file.

Facilitation TipFor the Small Group Note-Taking App, require groups to swap laptops midway to inspect each other’s file contents and modes used.

What to look forAsk students to write a short Python snippet that creates a file named 'my_notes.txt' and writes the line 'First note.' into it. Observe students' code for correct use of open(), write(), and close() functions.

ApplyAnalyzeEvaluateCreateSelf-ManagementRelationship SkillsDecision-Making
Generate Complete Lesson

Activity 03

Project-Based Learning25 min · Individual

Individual Debug: Write vs Append

Each student codes two functions: one overwrites 'test.txt' with new data, one appends. Input varied data, inspect files after each run. Log observations in a table.

Compare the implications of 'writing' versus 'appending' data to a file.

Facilitation TipIn the Individual Debug activity, provide a printout of the error messages so students can annotate corrections before editing their code.

What to look forPose the question: 'Imagine you have a file with student names and their test scores. If you want to add a new student's score without deleting the existing ones, what Python command should you use? What if you wanted to update a student's score with a new one, replacing the old score?'

ApplyAnalyzeEvaluateCreateSelf-ManagementRelationship SkillsDecision-Making
Generate Complete Lesson

Activity 04

Project-Based Learning20 min · Whole Class

Whole Class: File Review Relay

Project one shared file; class suggests inputs via mini whiteboards. Teacher runs code live, class predicts file changes. Vote on 'w' or 'a' for scenarios.

Justify the need for data persistence in applications like a high-score tracker.

What to look forProvide students with a scenario: 'You are building a simple diary application. What file mode would you use to add a new entry each day without losing previous entries, and why?' Students write their answer on a slip of paper.

ApplyAnalyzeEvaluateCreateSelf-ManagementRelationship SkillsDecision-Making
Generate Complete Lesson

A few notes on teaching this unit

Teachers should model the complete workflow: open, write, close, verify. Avoid demonstrations that skip the file explorer step—students need to see the physical artifact to believe persistence. Emphasize that 'w' mode is destructive; use metaphors like 'overwriting a journal page' to make the risk concrete.

Students will confidently choose between 'w' and 'a' modes, convert data to strings, and verify file contents after each activity. Successful work includes correctly saved data, proper file closing, and clear explanations of when to overwrite versus append.


Watch Out for These Misconceptions

  • During the Pair Challenge, watch for students who assume the file updates automatically without calling write() or close().

    Ask pairs to open the file in a text editor after each input step to confirm no changes appear until write() is called, reinforcing the need for explicit write and close calls.

  • During the Small Group Note-Taking App, watch for students who believe 'w' mode appends content to existing files.

    Have groups save identical files using both 'w' and 'a' modes side-by-side, then open both in a text editor to observe that only the 'a' file preserves old content.

  • During the Individual Debug Write vs Append activity, watch for students who try to write non-string data directly to files.

    Ask students to run their code and observe the TypeError, then guide them to insert str() conversions before write(), modeling the error-handling process.


Methods used in this brief