GCSE · Computer Science · Eduqas · Spec C500QS
File handling: read into an array
Print a line you read from a file and a blank line appears that was never there. One invisible character is to blame. Let's catch it.
Computer Science · Files
Read pets.txt into a list, one line at a time
pets.txt has three lines: cat, dog and fox. Version A: Enter was pressed after every line. Version B: Enter was pressed after cat and dog, but nobody pressed it after fox. Pick a version, then commit to each marked value before the table shows it.
items = []file = open("pets.txt")for line in file:items.append(line)file.close()print(len(items))first = items[0]
| line | items | first |
|---|---|---|
| [] |
Step 1. Before the loop, items is an empty list. This first step is worked for you.
Computer Science · Strings and numbers
Press Run: why will it not add up?
scores.txt holds three scores, one per line: 12, 15 and 9. The program should print their total, 36. Run it, read what happens, then choose the fix for the flagged line.
- file = open("scores.txt")
- total = 0
- for line in file:
- total = total + line
- file.close()
- print(total)
The messages and outputs are what Python 3.13 gives for this exact code, with scores.txt holding the whole numbers 12, 15 and 9 on separate lines.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Get a text file into a Python list, one line at a time, and meet the invisible \n on the way.
What you need to know
- Reading a file into an array means filling a Python 3 list, usually one item per line of the file.
- Loop over the open file with for line in file: and each pass hands you one line, as a string.
- Start with items = [], then items.append(line) adds each line to the end of the list.
Have a goA file has 6 lines. The loop runs items.append(line) once for every line. How long is the list at the end?
6 items.
One append per pass and one pass per line, so the list ends up as long as the file has lines.
- Afterwards, items[0] is the first item (counting starts at 0) and len(items) says how many items you read.
- Each line normally arrives with a hidden \n on the end; the last line of a file may not have one.
Have a goYour classmate says the first line, cat, is stored as a three-letter string. Count honestly: how many characters is the stored line?
Four: c, a, t and the newline.
The \n is a real character that comes with the line, even though you cannot see it. (The last line of a file may not have one.)
- readlines() builds the list for you; read() gives the whole file as one string, and splitlines() on that string drops the \n.
- strip() removes whitespace at both ends, \n included; rstrip("\n") removes only the trailing newline.
- When a line holds several items, line.split(",") cuts it into a list, so clean the \n off first or the last item keeps it.
- Split every line and append the result and you get a list of lists: records[i][j] is item j of line i.
- Everything read from a file is a string, so convert a numeric item with int() or float() before calculating or comparing.
Have a goThe line from the file is 7. Your program works out line + 1. Does that give 8?
No. It stops with a type error.
The 7 read from a file is a string, and a string cannot be added to a number. You would need int(line) + 1.
The big picture
A file read into Python becomes a list of strings, usually one per line. Each string normally still carries its \n, and numbers arrive as text, so you clean lines with strip() or rstrip("\n") before splitting them and convert numeric items with int() or float() before using them.
Key points
Worked example
Problem
basket.txt holds three lines: apples, bread and milk. Nobody pressed Enter after milk. Read it into a list with the line endings removed, then say what len(items) and items[1] are.
⚠ Watch out
Thinking each line arrives exactly as it looks in the file, with numbers already numbers. In fact every line is a string that normally ends in \n, so clean it before splitting and convert numbers before using them.
Memory hook
Every line wears an invisible \n, and is always text: strip it, split it, then convert it.
Check yourself
scores.txt has three lines: 4, 7 and 12. The loop appends each line with no cleaning. What does items hold, and what must you do before adding the scores?
Flashcards
(13)In Python 3, what does reading a file into an array mean?
What is the most common way to fill the list from a file?
What does for line in file: give you on each pass?
Starting from items = [], how does a line get into the list?
What does the readlines() method return?
What is \n, and where does it hide?
strip() or rstrip("\n"): what is the difference?
What do read() and splitlines() give you together?
What does "Sam,15,Wales".split(",") give?
In a list of lists, what does records[i][j] mean?
Which goes first, stripping the \n or splitting the line?
What type is a number you read from a text file, and how do you fix it?
Once the list is built, what do items[0] and len(items) give?
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 libraries
- Decomposition
- Designing a testing strategy
- Explaining test outcomes
- File handling: open a file
- File handling: read into a variable
- 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
- Presenting test outcomes with commentary
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.