Skip to content
Computer Science · Class 12

Active learning ideas

CSV File Operations: Writing and Updating

Active learning helps students grasp CSV operations because writing and updating files requires hands-on handling of data structures and file management. When students write and test code in real time, they immediately see how CSV files behave differently from spreadsheets or databases, making abstract concepts concrete through practice.

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

Activity 01

Pair Programming: Dictionary to CSV Writer

Pairs create a list of student dictionaries with keys like name, marks, grade. One partner dictates the code using csv.DictWriter, the other types and runs it to generate a CSV file. Switch roles, then verify output with a text editor and discuss header handling.

Construct a Python script to write a list of dictionaries to a CSV file.

Facilitation TipIn Pair Programming: Dictionary to CSV Writer, circulate and ask each pair to explain one line of their code to you before they run it, ensuring understanding of csv.writer and dictionary keys.

What to look forProvide students with a predefined list of dictionaries. Ask them to write the Python code to save this list to a CSV file named 'students.csv' with appropriate headers. Check if the file is created and contains the data correctly formatted.

ApplyAnalyzeEvaluateCreateRelationship SkillsDecision-MakingSelf-Management
Generate Complete Lesson

Activity 02

Collaborative Problem-Solving45 min · Small Groups

Small Groups: Row Update Challenge

Groups read a sample CSV of inventory data, search for a product ID, update its price in a list, and rewrite the file. Compare results if done naively versus with a temporary file. Note time and memory differences.

Analyze the challenges of updating specific rows in a CSV file directly.

Facilitation TipFor Small Groups: Row Update Challenge, provide a printed sample CSV so groups can annotate it with their update strategy before coding, reducing trial-and-error errors.

What to look forPresent a scenario: 'You have a CSV file of employee records, and you need to change the salary of one employee. Explain in 2-3 sentences why directly editing the file might be problematic and what alternative approach you would use.'

ApplyAnalyzeEvaluateCreateRelationship SkillsDecision-MakingSelf-Management
Generate Complete Lesson

Activity 03

Collaborative Problem-Solving40 min · Whole Class

Whole Class: Large Dataset Simulation

Teacher shares a 1000-row CSV script. Class follows along to write it, then updates 10% of rows using temp files. Students vote on efficiency via show of hands and share one lesson learned.

Justify the use of temporary files for modifying large CSV datasets.

Facilitation TipDuring Whole Class: Large Dataset Simulation, project the dataset on the board and simulate the memory constraints by limiting the number of rows groups can load at once, making the inefficiency of full rewrites tangible.

What to look forStudents write a script to update a specific row in a sample CSV (e.g., change a student's grade). They then swap scripts with a partner. Each partner reviews the code for logical errors, correct file handling (opening, closing, using temporary files if applicable), and provides one suggestion for improvement.

ApplyAnalyzeEvaluateCreateRelationship SkillsDecision-MakingSelf-Management
Generate Complete Lesson

Activity 04

Collaborative Problem-Solving25 min · Individual

Individual: Debug and Optimise

Each student gets a buggy update script with errors like unclosed files or wrong modes. Fix it, test on provided data, and add temp file logic. Submit optimised version with comments.

Construct a Python script to write a list of dictionaries to a CSV file.

What to look forProvide students with a predefined list of dictionaries. Ask them to write the Python code to save this list to a CSV file named 'students.csv' with appropriate headers. Check if the file is created and contains the data correctly formatted.

ApplyAnalyzeEvaluateCreateRelationship SkillsDecision-MakingSelf-Management
Generate Complete Lesson

A few notes on teaching this unit

Teachers should model the entire CSV writing process step by step, including header handling and quoting, while narrating each decision aloud. Avoid demonstrating 'magic' functions without breaking them down, as students need to see the mechanics of file handling to debug their own scripts. Research shows that students learn file operations best when they experience firsthand the consequences of forgetting to close files or mismanaging delimiters, so planned errors in live coding help solidify correct practices.

By the end of these activities, students should confidently write CSV files from dictionaries, explain why direct row updates are inefficient, and use temporary files or in-memory edits to modify data safely. Successful learning is visible when scripts run without errors and produce correctly formatted files that can be opened in any spreadsheet application.


Watch Out for These Misconceptions

  • During Pair Programming: Dictionary to CSV Writer, some students may assume CSV files can be edited like spreadsheets, leading them to attempt direct row modifications.

    Have pairs attempt to write a script that directly changes a row in the CSV file without reading the entire file first. When their script fails, guide them to observe the error message and discuss why CSV files require full reads and rewrites, then provide a corrected version using a temporary file approach.

  • During Pair Programming: Dictionary to CSV Writer, students might use print() statements to output data, assuming it formats correctly for CSV.

    Ask pairs to compare the output of print() and csv.writer() side by side on a small dataset. Have them note differences in line breaks and quotes, then modify their script to use csv.writer() exclusively, reinforcing proper CSV formatting.

  • During Individual: Debug and Optimise, students may omit closing files, assuming Python handles it automatically.

    Provide a script with an intentionally missing file close statement and simulate a crash mid-write. Students will observe incomplete or corrupted files, leading them to revise the script with a with statement or explicit close() call, and explain why it matters in their reflection.


Methods used in this brief