New
New
Year 11
OCR

Writing to text files

I can write data to a new file and append data to an existing text file.

New
New
Year 11
OCR

Writing to text files

I can write data to a new file and append data to an existing text file.

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. There are different modes for opening a file, r (read), w (write) and a (append).
  2. When you open a file in write mode, it creates a new file. If the file already exists then the file will be overwritten.
  3. When you open a file and write to a file in append mode, it will add to the existing contents of the file.
  4. It is good practice to close the file once written to. Some IDEs will not write to a file until it is closed.

Keywords

  • Write - a file handling mode that allows a program to write data to a file, where, if the file does not exist, a new file is created, and if the file already exists, the content is overwritten

  • Append - a file handling mode that allows a program to write extra data at the end of a file without erasing the existing contents of the file

  • Overwrite - to replace information in (a computer file) with new information

Common misconception

You have to create a new write() line for each line you want to add to a file.

You can write mutiple lines in one write() statement. But if you want the text to appear on separate lines, you must use the new line character.


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

This lesson includes some live coding video clips from The Raspberry Pi Code Editor, if your pupils are using a different IDE you may want to model in that environment instead.
Teacher tip

Equipment

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.
Which file mode is used to read a file in Python?
w
Correct answer: r
a
rw
Q2.
To read a text file line by line, you should use the method.
Correct Answer: readline(), readline
Q3.
Which method returns all lines of a file as a list?
.read()
.readline()
Correct answer: `.readlines()`
.readfile()
Q4.
Which of the following is the correct way to read the entire content of a text file in Python?
Correct answer: `file = open("data.txt", "r") content = file.read() file.close()`
file = open("data.txt", "w") content = file.read() file.close()
file = open("data.txt", "r") content = file.readline() file.close()
Q5.
Which is the Python method which removes any leading and trailing whitespaces or characters that you specify?
remove()
replace()
Correct answer: `strip()`
Q6.
Saving program data to an external allows you to use that data again.
Correct Answer: file

6 Questions

Q1.
What happens when you open a file in write ("w") mode?
The file is opened for reading only.
Correct answer: A new file is created, or an existing file is overwritten.
Data is added to the end of an existing file.
The file is deleted.
Q2.
To add new data to an existing file without erasing its content, you should open the file in mode.
Correct Answer: append, a
Q3.
What will the following code do?
123
file = open("example.txt", "w") file.write("Hello, world!") file.close()
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
Correct answer: It will create a file (or overwrite an existing one) and write "Hello, world!"
It will append "Hello, world!" to an existing file.
It will delete the file.
It will print "Hello, world!" to the console.
Q4.
To make sure changes are saved when writing to a file, it is good practice to the file after writing.
open
delete
save
Correct answer: close
Q5.
Which of the following is the correct way to open a file, write text to it, and close it properly in Python?
file = open("data.txt", "w") file.write("Hello!")
Correct answer: `file = open("data.txt", "w") file.write("Hello!") file.close()`
file.write("Hello!") file = open("data.txt", "w") file.close()
Q6.
Which character is used to ensure text is added to a new line in a file?
\l
/n
\s
Correct answer: `\n`