Skip to content
Computer Science · Grade 11

Active learning ideas

Data Persistence: File I/O

File I/O concepts stick when students physically handle files and see real failures. Active tasks like parsing CSVs or hunting errors make abstract persistence visible, turning silent crashes into teachable moments. This approach builds confidence because students debug their own code instead of watching demonstrations.

Ontario Curriculum ExpectationsCS.HS.D.5CS.HS.P.5
25–45 minPairs → Whole Class4 activities

Activity 01

Flipped Classroom35 min · Pairs

Pair Programming: CSV Grade Analyzer

Pairs read a CSV of student scores, compute class averages using lists, and write results to a summary text file. Start by importing csv and handling open errors with try-except. Test by swapping valid and invalid files, then discuss fixes.

Explain the importance of error handling when performing file input/output operations.

Facilitation TipDuring Pair Programming, assign clear roles: one driver writes code while the navigator checks syntax and anticipates errors.

What to look forProvide students with a small, pre-made CSV file. Ask them to write pseudocode or a short Python snippet that reads the file, calculates the average of a specific column, and prints the result. Include a prompt: 'What is one potential error that could occur, and how would you handle it?'

UnderstandApplyAnalyzeSelf-ManagementSelf-Awareness
Generate Complete Lesson

Activity 02

Flipped Classroom45 min · Small Groups

Small Groups: Error Hunt Challenge

Provide code snippets with common file I/O bugs like unclosed files or path errors. Groups trace issues, add handling, and run tests on shared drives. Debrief by voting on trickiest fixes.

Compare the advantages and disadvantages of storing data in text files versus a database.

Facilitation TipIn Error Hunt Challenge, provide code snippets with deliberate mistakes so groups diagnose and fix them collaboratively.

What to look forDisplay a code snippet that attempts to read from a non-existent file without error handling. Ask students: 'What will happen when this code runs? What keyword or structure should be added to prevent a crash?'

UnderstandApplyAnalyzeSelf-ManagementSelf-Awareness
Generate Complete Lesson

Activity 03

Flipped Classroom25 min · Individual

Individual: Log File Parser

Students read a simulated server log text file, count error entries with string methods, and write a report CSV. Include deliberate bad paths for self-debugging. Submit code with sample outputs.

Construct a program that reads data from a CSV file, processes it, and writes it to another file.

Facilitation TipFor Log File Parser, give students a partially corrupted log to parse, forcing them to handle edge cases like empty lines or malformed entries.

What to look forPose the question: 'Imagine you have a large dataset of student grades. Would you store this in a simple text file, a CSV file, or a database? Explain your reasoning, considering factors like ease of access, potential for errors, and scalability.'

UnderstandApplyAnalyzeSelf-ManagementSelf-Awareness
Generate Complete Lesson

Activity 04

Flipped Classroom40 min · Whole Class

Whole Class: Data Pipeline Build

Project a shared screen to co-create a program: read inventory CSV, update stock levels, write new file. Pause for predictions on errors, vote on solutions, and run live.

Explain the importance of error handling when performing file input/output operations.

Facilitation TipDuring Data Pipeline Build, require groups to document each step’s purpose and error-handling strategy in a shared table.

What to look forProvide students with a small, pre-made CSV file. Ask them to write pseudocode or a short Python snippet that reads the file, calculates the average of a specific column, and prints the result. Include a prompt: 'What is one potential error that could occur, and how would you handle it?'

UnderstandApplyAnalyzeSelf-ManagementSelf-Awareness
Generate Complete Lesson

A few notes on teaching this unit

Teach file I/O by starting with small, predictable files before scaling to CSVs or logs. Model how to read line-by-line and parse with split() before introducing the csv module. Explicitly contrast text files with databases by timing operations on the same dataset. Emphasize that error handling isn’t optional—it’s part of the design, not an afterthought. Research shows students grasp persistence better when they experience crashes and fix them themselves rather than reading about try-except blocks in isolation.

Students will demonstrate fluency in reading, parsing, and writing files with error handling. They will justify design choices between text files, CSVs, and databases based on context. Clear evidence appears in working code, error-free runs, and thoughtful discussions about trade-offs.


Watch Out for These Misconceptions

  • During Pair Programming: CSV Grade Analyzer, students assume files always open successfully without checks.

    Circulate during the activity and deliberately remove a file from one pair’s folder. Pause the class to observe the crash, then guide the pair to add a try-except block that prints a user-friendly message instead of halting the program.

  • During Error Hunt Challenge, students believe text files work for all data volumes like databases.

    Provide large CSV files to half the groups and have them time the parse operation. Ask groups to compare results with a mock database query, then facilitate a discussion on scalability limits and when files are no longer sufficient.

  • During Log File Parser, students forget to close files and assume it has no impact.

    Simulate a resource leak by having groups run their parsers in a loop that opens the same file repeatedly. Use system monitoring tools to show memory usage spikes, then guide students to refactor with a with statement for automatic closure.


Methods used in this brief