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 and append file modesChoose the file mode from the required effect"w" · writecreate or replace file contentold content may be lostfile.write(record + "\n")"a" · appendkeep existing contentadd new text at the endfile.write(record + "\n")Close the file after the operation so buffered data is written safely.

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.

Fill in the blanks3 marks
Mode gap 1 replaces existing content, while mode gap 2 adds at the end. A record separator can be written using gap 3.
  • "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()
Multiple choice1 mark

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
Written answer2 marks

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().

Coding task5 marks
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.

Multiple choice1 mark

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
Written answer2 marks

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.

Written answer2 marks

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.

Multiple choice1 mark

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.