GCSE · Computer Science · Eduqas · Spec C500QS

File handling: open a file

One wrong letter in one line of code, and your saved high scores are gone before anything is written.

Computer Science · File handling

Same program, same file: what does each mode do?

The user types a mode. The program opens scores.txt, adds one score, then reads the file back. Call the stored file before the page shows it.

Pick a run, then step through it. At each marked line, call what is stored in scores.txt (or what is printed) before the page shows it.

mode = input("Mode (r, w or a): ")
myFile = open("scores.txt", mode)
myFile.write("Zara 70\n")
myFile.close()
myFile = open("scores.txt", "r")
print(myFile.read())
myFile.close()

Variables

mode=ascores.txt (stored)=Ali 90, Ben 85

Step 1. The user types a. scores.txt already exists and holds two scores.

Step 1 of 7Called right: 0 of 2

The line "scores.txt (stored)" is what is saved on disk, not what is in memory.

Predict, then check

One short line of code. Three things happen when it runs.

A program runs this line: myFile = open("scores.txt"). Which description is right?

Computer Science · File modes

Build the mode grid

For each mode, tick every behaviour it has when you open a file with it. Then check your grid.

Mode "r"
Mode "w"
Mode "a"

Exam line: "w" and "a" both create a missing file. Only "w" erases what is already stored.

Computer Science · Choosing a mode

Which mode does the job need?

Each job below is an example. Put it with the one mode it needs.

Still to sort

Mode "r" (0)

Read data that is already stored

Where the line is: Choose "r" when nothing in the file should change.

Mode "w" (0)

Create a file or replace its whole contents

Where the line is: Choose "w" only when losing the old contents is the point.

Mode "a" (0)

Add to the end of what is stored

Where the line is: Choose "a" when the old contents must survive.

6 of 6 still to sort.

Computer Science · Spot the fault in code

The vanishing scores

This program should add Zara's score to the end of the scores already stored in scores.txt. After it runs, the old scores have vanished. Which line is the fault?

A student's program — which line goes wrong?

Exam line: If a file must keep what it already holds, open it in "a", not "w".

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

One short line of code decides whether your saved data survives. Learn what each mode does the moment a file is opened.

What you need to know

  • A program must open a file before it can read from it or write to it. Opening connects the program to the stored file.
  • In Python 3 you use open(), giving it the file name as a string and a mode string. It returns a file object.
  • A file name with no folder path is looked for in the program's current working directory.
  • Have a goYour scores.txt lives in a folder called Games. Your program is running from a different folder and says open("scores.txt"). Which folder does Python 3 search?

    The program's current working directory, not Games.

    A name with no folder path is only looked for in the current working directory. Python does not go searching for it.

  • Mode "r" is for reading only. It is the default, and it leaves the contents unchanged.
  • Open a file that does not exist in mode "r" and Python raises a FileNotFoundError.
  • Mode "w" creates a missing file. If the file exists, "w" erases its contents the moment it is opened.
  • Have a goDev says: "I opened scores.txt with 'w' but I haven't written anything yet, so my old scores are still safe." Is Dev right?

    No. The old scores were erased as soon as the file was opened.

    It feels as if erasing should need a write, but "w" empties an existing file on the open line itself.

  • Mode "a" also creates a missing file, but an existing file keeps its contents and new data goes on the end.
  • Have a goscores.txt holds Ali 90. A program opens it in "a" and writes Ben 85. What does the file hold, in order?

    Ali 90, then Ben 85.

    Mode "a" keeps what is already stored and puts new data on the end, so Ben goes after Ali.

  • Pick the mode from the job: "r" to read, "w" to create or replace everything, "a" to add without losing anything.
  • Using "w" when you needed "a" permanently loses the stored data. "r" and "a" do not remove it.

The big picture

Python 3 opens a file with open(), which takes a file name and a mode and returns a file object. The mode decides what happens to the file at the moment it is opened: "r" reads and leaves it alone, "w" creates it or erases it, and "a" creates it or keeps it and adds to the end.

Key points

1open() takes a file name and a mode and returns a file object.
2No mode means "r". No folder path means the current working directory.
3"w" and "a" both create a missing file. Only "w" erases an existing one.
4"r" on a missing file raises a FileNotFoundError.

Worked example

Problem

A music app keeps its playlist in playlist.txt. Each time the app runs it must add one new song title to the end, keeping every title already saved. The first time it ever runs, playlist.txt may not exist yet. Write the line that opens the file.

⚠ Watch out

Thinking "w" only erases a file when you write to it. The erasing happens on the open line, before a single character is written. If the old data must survive, open with "a".

🧠

Memory hook

r reads, w wipes, a adds.

✓

Check yourself

A friend's program loses its saved data every time it runs. Which line would you check first, and what would you hope to see there?

Flashcards

(11)
Why must a program open a file first?
Opening connects the program to the stored file. Without it the program cannot read data from the file or write data to it.
Which Python 3 function opens a file, and what does it need?
open(). It takes the file name as a string and a mode string, e.g. open("scores.txt", "r").
What does open() give back?
A file object. The program uses it to work with the file.
A file name has no folder path. Where does Python 3 look for it?
In the program's current working directory.
Which mode does open("scores.txt") use?
Mode "r", the default when no mode is given.
What happens if you open a missing file in mode "r"?
Python raises a FileNotFoundError.
When does mode "w" erase an existing file's contents?
As soon as the file is opened, before anything is written.
What does mode "a" do to a file that already exists?
Keeps the existing contents. Anything written is added to the end.
Which two modes create a file that does not exist?
"w" and "a". Mode "r" raises a FileNotFoundError instead.
Which mode do you choose to create a file or replace its whole contents?
Mode "w".
What goes wrong if you open with "w" when "a" was needed?
The data already stored in the file is permanently lost.

Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.

Learning with Lightbulb is opening soon

You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.

Keep me posted

More Eduqas GCSE Computer Science topics

See the full Eduqas Computer Science curriculum →

How this lesson was checked. This Eduqas GCSE Computer Science (specification C500QS)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 16 November 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.