New
New
Year 11
OCR

Reading text files

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

New
New
Year 11
OCR

Reading text files

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

These resources will be removed by end of Summer Term 2025.

Switch to our new teaching resources now - designed by teachers and leading subject experts, and tested in classrooms.

These resources were created for remote use during the pandemic and are not designed for classroom teaching.

Lesson details

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.


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

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.
Teacher tip

Equipment

All pupils require access to devices that can edit and run Python programs. Starter code files are available to copy or use directly via the Raspberry Pi Code Editor.

Licence

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

Lesson video

Loading...

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

When programmers write code, they use a special tool called an IDE (Integrated Development Environment). In an IDE, different colours are used to help programmers understand the code:


  • • Blue - numbers and boolean values
  • • Green - strings
  • • Purple - keywords
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

When programmers write code, they use a special tool called an IDE (Integrated Development Environment). In an IDE, different colours are used to help programmers understand the code:


  • • Blue - numbers and boolean values
  • • Green - strings
  • • Purple - keywords
[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