Writing records to a CSV file
Joins a record's fields, which must all be strings, into one comma-separated line. Use str() on numbers first, then write the line plus a newline to the CSV file.
Understanding Writing records to a CSV file in VCE Software Development U3O1
What it does: The header row is just a comma-separated line of column names written before any records, ending with a newline. Mode 'a' means append, so new lines are added to the end. Mode 'w' would overwrite the existing data. The separator comes first: ','.join(record) puts a comma between each field in the list. record.join(',') is wrong because lists do not have a join method. join() only accepts strings. The number 16 must be converted with str() first, otherwise Python raises a TypeError. Every field must be a string. str(16) turns the integer into '16', so join() can handle it. 'a' appends to the end of the file. 'w' overwrites the file, so all earlier records would be lost.
Common mistakes: Mode "r" opens the file for reading only, so f.write() would fail. To save records the file must be opened in write mode: open("pupils.csv", "w"). 15 is an integer, and join() only accepts strings, so line 2 would crash with a TypeError. Convert the number first with str(15).
Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.
This module's key terms are also in a printable crossword (Modules 98–113) and word search (Modules 72–113).
Take this worksheet further — free with Cloodit
Each concept is reinforced from every angle — learned, then applied, then tested.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
','.join(record)Fill in the Blanks
Spot the Error
Student with a class code? Join your class →