Skip to content
Computer Science · Grade 9

Active learning ideas

File Input/Output

Active learning works for file input/output because students need to experience the frustration of missing files or overwritten data to truly grasp why error handling matters. These hands-on activities let students crash programs intentionally, then rebuild them with safeguards, turning abstract concepts into lasting understanding through trial and error.

Ontario Curriculum ExpectationsCS.HS.AP.13CS.HS.CT.14
20–45 minPairs → Whole Class4 activities

Activity 01

Project-Based Learning30 min · Pairs

Pair Programming: Score Logger

Pairs write a program that prompts for a name and score, appends it to a text file, then reads and prints all scores. Add try-except for file errors. Pairs test by running multiple times and swapping files to simulate shared data.

Analyze the process of reading data from a file and writing data to a file.

Facilitation TipDuring Pair Programming: Score Logger, circulate to ensure both partners take turns driving and navigating, reinforcing collaboration while they debug file errors together.

What to look forPresent students with a short Python code snippet that attempts to read from a file. Ask them to identify potential errors and explain what might happen if the file does not exist or is empty. 'What is the expected output of this code? What happens if 'my_data.txt' is missing? How could we prevent this error?'

ApplyAnalyzeEvaluateCreateSelf-ManagementRelationship SkillsDecision-Making
Generate Complete Lesson

Activity 02

Project-Based Learning45 min · Small Groups

Small Groups: Contact Saver

Groups build a command-line app to add contacts (name, email), save to a file, and load/display them. Include options to view or quit. Groups exchange files mid-activity to test robustness.

Design a program that saves user-generated data to a file and loads it later.

Facilitation TipFor Small Groups: Contact Saver, provide pre-prepared contact lists with intentional duplicates or missing fields to spark discussions about data validation.

What to look forProvide students with a scenario: 'You are building a simple to-do list application. Describe in 2-3 sentences how you would use file operations to save the user's list so it is available the next time they open the app.' Collect these to gauge understanding of data persistence.

ApplyAnalyzeEvaluateCreateSelf-ManagementRelationship SkillsDecision-Making
Generate Complete Lesson

Activity 03

Project-Based Learning20 min · Individual

Individual: Error Debug Challenge

Provide buggy code with common file I/O errors like unclosed files or wrong modes. Students fix and test individually, then share one fix with the class. Emphasize try-except blocks.

Justify the importance of error handling when working with file operations.

Facilitation TipIn the Individual: Error Debug Challenge, give students broken code with clear indicators of where errors should occur, so they focus on fixes rather than hunting for issues.

What to look forFacilitate a class discussion: 'Imagine you are designing a system to log failed login attempts for a website. Why is it important to handle errors when writing to this log file? What are two specific errors you might encounter and how would you address them?'

ApplyAnalyzeEvaluateCreateSelf-ManagementRelationship SkillsDecision-Making
Generate Complete Lesson

Activity 04

Project-Based Learning25 min · Whole Class

Whole Class: Persistence Demo

Class runs a shared program that logs attendance to one file; students take turns inputting data and reading back. Discuss cybersecurity implications like tamper detection.

Analyze the process of reading data from a file and writing data to a file.

Facilitation TipFor the Whole Class: Persistence Demo, use a live-coding approach to model proper file handling, making visible every keystroke and error so students see the process in real time.

What to look forPresent students with a short Python code snippet that attempts to read from a file. Ask them to identify potential errors and explain what might happen if the file does not exist or is empty. 'What is the expected output of this code? What happens if 'my_data.txt' is missing? How could we prevent this error?'

ApplyAnalyzeEvaluateCreateSelf-ManagementRelationship SkillsDecision-Making
Generate Complete Lesson

A few notes on teaching this unit

Teach file I/O by starting with small, controlled mistakes so students feel the consequences of poor practices. Avoid overwhelming them with large files or complex data early on. Research shows students learn file operations best when they connect the code to real-world persistence needs, like saving a game or logging an event, so frame activities around relatable scenarios they care about.

Students will demonstrate understanding by writing code that handles file operations safely, explaining why certain modes or error checks are necessary, and applying persistence concepts in practical scenarios. Success looks like students debugging their own code, justifying their choices, and transferring these skills to new contexts without prompting.


Watch Out for These Misconceptions

  • During Pair Programming: Score Logger, watch for students assuming files exist and can be read without checks.

    During this activity, have partners deliberately delete the score file mid-session to expose the crash, then rewrite the code together using try-except blocks to handle missing files gracefully.

  • During Small Groups: Contact Saver, watch for students believing 'w' mode always appends new data.

    During this activity, provide sample files and have groups experiment with both 'w' and 'a' modes, observing how 'w' overwrites data and prompting a class discussion on mode selection.

  • During Individual: Error Debug Challenge, watch for students reading entire files at once for every task.

    During this activity, challenge students to time both reading methods on a growing file, then justify which method they would use for a 100,000-line file, reinforcing efficiency choices.


Methods used in this brief