GCSE · Computer Science · Eduqas · Spec C500QS
File handling: read into a variable
You read a name from a file and compare it with what the user typed. The program insists they differ. So what is hiding in that variable?
Computer Science · Reading a file
What does each readline() hand back?
pets.txt holds three lines: cat, dog, fox. The first two end in a newline. The last, fox, has no newline after it. Step through, and call each string before the table shows it. Nothing is run: every value is the lesson's.
f = open("pets.txt", "r")line = f.readline() # call 1line = f.readline() # call 2line = f.readline() # call 3line = f.readline() # call 4if line == "":print("No more lines")
Variables
Step 1. We open pets.txt in read mode. f is the file object we read through. Nothing has been read yet, so there is no line variable. This first step is worked for you.
Computer Science · Reading numbers from a file
The total that came out wrong
scores.txt has a 5 on its first line and a 3 on its second. This program should print their total. Press Run, read what comes back, then pick the replacement for line 4.
- f = open("scores.txt", "r")
- a = f.readline().strip()
- b = f.readline().strip()
- print(a + b)
The outputs and messages are what Python 3 gives for exactly this code.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Get a file's contents into your program, and spot the surprises that come with them.
What you need to know
- Reading a file copies its data into the running program's memory, where a variable holds it so the program can process it.
- In Python 3 you read through the file object of a file that has already been opened for reading, in read mode "r".
- f.read() returns the whole contents of the file as one single string, so contents = f.read() stores it in a variable.
- That string keeps a newline, "\n", at the end of each line that ends in one. The last line may have no "\n".
Have a goA classmate says, completely confidently: "If my file has two lines, f.read() gives me a list of two strings." What does it really give?
One single string, with the \n of line 1 sitting inside it.
read() returns the whole contents as a single string. The newlines are just characters inside it, not the edges of list items.
- f.readline() returns the next single line as a string, including its "\n" if the line has one.
- Each readline() call moves on through the file, so successive calls return successive lines.
- At the end of the file, readline() and read() return the empty string "", which a program can test for.
- A loop such as for line in f: reads one line at a time and puts each line in turn into line.
Have a goA file holds red, green and blue, one per line, each line ending in a newline. In for line in f:, what exactly does line hold on the second pass?
"green\n"
The loop puts each line in turn into line, and a line that ends in a newline keeps its \n. The second pass gives the second line.
- strip() (or rstrip("\n")) removes the trailing newline: name = f.readline().strip(). Do it before you display, compare or store the value.
- Text read from a file is always a str, even when it looks like a number. Cast it with int() or float() to use it as one.
Have a goYour friend says score = f.readline() "obviously gives me a number", because the line is only digits. What type is score, and what fixes it?
It is a str. Cast it: score = int(f.readline()).
Data read from a text file is always a str, even when the characters represent a number. int() converts it, and float() would do it for a real number.
The big picture
Reading a file copies its data into the program's memory, where a variable holds it as a string. read() hands over the whole file at once. readline() and a for loop hand over one line at a time, newline included. At the end of the file you get the empty string. Strip the newline and cast the text before you use it as a number.
Key points
Worked example
Problem
names.txt holds two lines, Ann and then Raj. Ann's line ends in a newline. Raj's line is the last and has no newline. After contents = f.read(), exactly what does contents hold, and how many characters is that?
⚠ Watch out
Treating file data as ready to use. A line arrives with its \n, numbers arrive as str, and each readline() call moves on instead of giving the same line again.
Memory hook
Every read hands you a str with its \n still on: strip it, then cast it, then use it.
Check yourself
Without looking back: a file has three lines and each one ends in a newline. Say what four successive readline() calls return. Then say how you would turn the second result into a whole number.
Flashcards
(13)What does reading a file do?
What must be true of a file before Python 3 can read it?
What does read() return?
Where do newline characters appear in the string from read()?
What does readline() return?
You call readline() three times in a row. What do you get?
What does readline() return at the end of the file, and why does that matter?
What does for line in f: do?
When does read() suit, and when does line-by-line suit?
How do you remove the trailing newline from a line you have read?
What data type is text read from a file?
How do you use a line from a file as a whole number or a real number?
What happens with int("ten")?
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 postedMore Eduqas GCSE Computer Science topics
- Creating and extending Python 3 functions/methods
- Creating and extending Python 3 libraries
- Decomposition
- Designing a testing strategy
- Explaining test outcomes
- File handling: open a file
- File handling: read into an array
- File handling: write to a file
- Iteration: condition and counter controlled loops
- Lists, tuples and dictionaries
- Mathematical and logical operations in code
- Modular structuring with abstraction
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.