Community resourceWorksheet
1CP2-CT-5.5 Writing and appending CSV files
Part 5 of 7 · 1CP2-CT-5 · Merge sort, files and authentication
The writing worksheet, and the difference between adding to a file and replacing it.
Students will:
- choose between write mode and append mode for a requirement
- predict the file contents a program produces
- create a comma-separated report from a structure
- explain what writelines does and does not add
- give a normal and a boundary test with expected file contents
Inside: 7 explanation cells, 3 multiple-choice questions, 1 Python task, 1 fill-in-the-blanks cell and 3 written answers. 17 marks, about 45 minutes.
Series: 1CP2-CT-5 · Merge sort, files and authentication, part 5 of 7.
Shared by Coding PathwayVerified teacher
- 15 cells
- About 45 minutes
- CC BY-SA 4.0
- Shared 17 Aug 2026
Preview
The whole resource, exactly as a class sees it. Answers and marking are held back.
Writing and appending CSV files
Writing sends program data to persistent text. The mode matters: "w" creates or replaces content, while "a" preserves existing content and adds at the end.
1. Select the safe mode
write() writes one string. writelines() writes a list of strings but does not insert separators for you. Include \n where one record should end and the next should begin, then close the file.
- "a"
- "r"
- "w"
- `\n`
2. Predict the effect
Assume scores.csv contains Ari,8\n. Then this program runs:
score_file = open("scores.csv", "a")
score_file.write("Bo,6\n")
score_file.close()
What will scores.csv contain afterwards?
- AOnly `Bo,6`
- B`Ari,8` followed by `Bo,6` on the next line
- COnly `Ari,8`
- DAn empty file
Explain what would change if mode "w" were used instead.
Refer to the existing record.
Students type their answer here.
3. Create a CSV report
Write every supplied record to report.csv in name,score form, one record per line. Convert each integer score to text and close the file. Finally reopen it in read mode and store its lines in saved_lines using readlines().
records = [["Ari", 8], ["Bo", 6], ["Cy", 9]]
report_file = open("report.csv", "w")
# Write every record, close, then read the file into saved_lines.
4. Understand writelines()
writelines(["A,1\n", "B,2\n"]) writes both strings. It does not automatically add commas or newline characters. Prepare each record exactly as it should appear.
What is written by file.writelines(["A,1", "B,2"])?
- ATwo separate lines
- B`A,1B,2` with no separator
- COnly `B,2`
- DNothing because the list is invalid
A program must preserve yesterday's log and add today's event. State the mode and explain why it is suitable.
Name the mode and its effect on existing content.
Students type their answer here.
5. Verify persistent output
A useful file-writing test checks the file itself, not only variables still in memory. Reopen the file in read mode and compare its exact contents with the expected records, separators and order.
Give one normal and one boundary test for a program that appends CSV records, including expected file contents.
Consider an ordinary record and a value at an allowed limit or an initially empty file.
Students type their answer here.
Why should numeric values usually be converted with str() before concatenation into a CSV line?
- AA CSV text file stores the line as text.
- BIt encrypts the number.
- CIt sorts the record.
- DIt changes append mode to write mode.
Route forward
You can create, replace and append CSV text safely. Next you will combine file reading, record processing and search to implement an ID-and-password authentication lookup.