Computer Science Essentials, Unit 3, Lesson 5 of 5

Records and files

GCSE Computer Science, Writing code, 35 minutes

Sign in

Reading and writing text files, splitting a line into the fields of a record, totalling, filtering and updating records, reading until a rogue value, and completing and refining a program.

What you will learn

  • Split a record into fields and convert number fields
  • Total, filter and pick the best record from a file
  • Update one record and keep the rest
  • Complete and refine a program one feature at a time

Files and records

A text file stores lines of text.

A record is one line that holds several fields, often separated by commas: Aisha,15,Leeds holds a name, an age and a town.

# Reading a file in Python
file = open("members.txt", "r")
for line in file:
    line = line.strip()
    print(line)
file.close()

# Writing to a file
file = open("scores.txt", "w")
file.write("Aisha,42\n")
file.close()

OCR style uses openRead, readLine(), endOfFile() and close(); AQA papers describe the file in words. In the questions here the lines have already been read into a list of strings, one string per line, so you can concentrate on what to do with them.

The rest of this lesson and its 9 practice questions are locked.

In this lesson

  1. Files and records
  2. Splitting a record
  3. Reading until a rogue value
  4. Complete and refine a program
  5. Try it: One field from a record

9 practice questions, marked as you go; about 35 minutes in all.

Key terms

Record, Fields

In this unit: Writing code

Write real Python that runs against tests: functions, validation and logins, strings, lists and 2D lists, records, and completing and refining a program.

All of Writing code