Community resourceWorksheet

1CP2-CT-5.3 Reading CSV text files

Part 3 of 7 · 1CP2-CT-5 · Merge sort, files and authentication

The file reading worksheet, following connect, read and close on a real comma-separated file.

Students will:

  • open a file in read mode and close it deliberately
  • predict what successive read calls return
  • explain why a further read past the end returns an empty string
  • read every line of a file in a loop
  • compare readline, readlines and a for loop over the file

Inside: 7 explanation cells, 3 multiple-choice questions, 2 Python tasks, 1 fill-in-the-blanks cell and 2 written answers. 15 marks, about 45 minutes.

Series: 1CP2-CT-5 · Merge sort, files and authentication, part 3 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.

Reading comma-separated text files

A file keeps data after a program finishes. Pearson's Programming Language Subset supports comma-separated value (CSV) text files and the operations open, read and close. This worksheet uses explicit open() and close() so each stage is visible.

1. Connect, read, close

Reading and processing a CSV text fileopen "r"connect to fileread lineone recordstripremove split(",")make fieldsconvert / usethen close fileCSV text becomes usable program data in stagessplit produces strings: convert numeric fields before arithmetic.

Read mode "r" does not change the file. readline() returns one line and returns an empty string at end of file. readlines() returns all remaining lines as a list. A for loop over the file reads one line at a time.

Fill in the blanks3 marks
Mode gap 1 opens a file for reading. readline() returns one gap 2, while readlines() returns a gap 3 of lines.
  • "a"
  • "r"
  • line
  • list
  • record

2. Predict a file read

Assume clubs.csv contains:

Chess,18
Robotics,12

The program is:

club_file = open("clubs.csv", "r")
first = club_file.readline()
second = club_file.readline()
club_file.close()
print(second)
Multiple choice1 mark

Which record does the program print?

  • A`Chess,18`
  • B`Robotics,12`
  • CBoth records as a list
  • DAn empty string
Written answer2 marks

Explain why calling readline() a third time would return an empty string.

Refer to the file position and end of file.

Students type their answer here.

3. Run an authentic read

Run the setup once. It creates the small CSV used by the following task. This cell prepares lesson data; it is not a worked answer.

Worked example
seed_file = open("activities.csv", "w")
seed_file.write("Chess,18\nRobotics,12\nCoding,21\n")
seed_file.close()
print("activities.csv is ready")

4. Read every line

Open activities.csv in read mode. Use a for loop to append each line to lines_read, close the file, then print the list. Keep the newline characters for now; the next worksheet will process them.

Coding task4 marks
activity_file = open("activities.csv", "r")
lines_read = []
# Read every line, close the file and print lines_read.

5. Close deliberately

Closing a file releases the operating-system resource and ensures the program is no longer relying on an open connection. In longer programs, leaving files open can waste resources or interfere with later access.

Written answer3 marks

Compare readline(), readlines() and a for loop over a file.

State what each returns or processes and when it may be useful.

Students type their answer here.

Multiple choice1 mark

Which statement about read mode is correct?

  • AIt appends a new line automatically.
  • BIt replaces existing file content.
  • CIt permits existing text to be retrieved without intending to change it.
  • DIt converts every field to an integer.
Multiple choice1 mark

What does a file line usually include at its end before processing?

  • AA Boolean
  • BA newline control character
  • CA password
  • DA list index

Route forward

You can open, read and close a CSV text file. Next you will remove line endings, split records into fields and convert numeric strings before calculation.