Choose exam board for KS4 Computer Science (GCSE)
Choose exam board for KS4 English
Choose exam board for KS4 French
Choose exam board for KS4 Geography
Choose exam board for KS4 German
Choose exam board for KS4 History
Choose tier for KS4 Maths
Choose exam board for KS4 Music
Choose exam board for KS4 Physical education (GCSE)
Choose exam board for KS4 Religious education (GCSE)
Choose exam board for KS4 Spanish

      Lesson details

      Learning outcome

      I can open and read data from a text file in a program.

      Key learning points

      1. A file is a term used to describe data that is held in storage on a computer.
      2. File-handling is a term used to describe how programs work with the files that are stored in a computer.
      3. Different file-handling modes allow you to perform different operations on the contents of a file.
      4. Saving program data to an external file allows the data to be reused when the program runs again.

      Keywords

      • File - a collection of data stored digitally, identified by a name

      • File-handling - an operation that is specified within a command to open a file, that determines how a file can be used

      • Strip() - a Python method which removes any leading and trailing whitespaces or characters that you specify

      Common misconception

      A text file can be stored anywhere to be used in a program using just the file name.

      A text file must be stored in the same location as a program to be used to be accessed by file name alone. If it is stored elsewhere, the full path of the file will also need to be included.

      Teacher tip

      If pupils are using The Raspberry Pi Code Editor they need to have text files included in the project in order to be able to use them. The sample code files already include text files for pupils to use.

      Equipment

      Pupils will need access to a device that can edit and run Python programs. Examples in this lesson use the RPF Code Editor https://oak.link/python-editor

      Licence

      This content is © Oak National Academy Limited (2026), licensed on Open Government Licence version 3.0
      except where otherwise stated. See Oak's terms & conditions
      (Collection 2).

      Lesson video

      Loading...

      Prior knowledge starter quiz

      6 Questions

      Q1.
      What type of brackets are used to define a dictionary in Python?

      ( )
      [ ]
      Correct answer: { }

      Q2.
      What type of brackets are used to define a list in Python?

      ( )
      Correct answer: [ ]
      { }

      Q3.
      What is an attribute in a record?

      a type of variable in Python
      a function that retrieves data
      Correct answer: a property or characteristic of an entity
      a key used to access lists

      Q4.
      List items are separated by in Python.

      Correct Answer: commas, ,

      Q5.
      What will be the output of this code?
      12
      player = { "username" : "rockstar", "password" : "6goatsEating", "score" : 5328 } print(player["score"])
      Code colour

      score
      player
      rockstar
      Correct answer: 5328

      Q6.
      What will be the output of this code?
      123
      my_list = [3, 6, 9, 12] my_list.append(15) print(my_list)
      Code colour

      [3, 6, 9, 12]
      Correct answer: `[3, 6, 9, 12, 15]`
      [15, 3, 6, 9, 12]
      [3, 6, 9, 15]

      6 Questions

      Q1.
      The method used to remove extra spaces or new lines from the beginning and end of a string in Python is called .

      Correct Answer: strip(), strip

      Q2.
      Opening a file in write (w) mode will...

      Correct answer: delete any existing content in that file.
      add to any existing content in that file.
      read the data in the file

      Q3.
      Which of the following correctly opens a file named "data.txt" in read mode?

      file = open("data.txt", "a")
      Correct answer: `file = open("data.txt", "r")`
      file = open("data.txt", "w")
      file = open("data.txt")

      Q4.
      Once you have finished working with a file, you should always it to free up system resources.

      Correct Answer: close, close()

      Q5.
      Which method reads an entire file and returns it as a string?

      Correct answer: `read()`
      readlines()
      readline()
      strip()

      Q6.
      Using readline() will...

      read the entire file
      Correct answer: read one line at a time
      read one character at a time

      To help you plan your 11 computer science lesson on: Reading text files, download all teaching resources for free and adapt to suit your pupils' needs...