Python Foundations

Text Files

Open, read, write and append text files in Python — then combine files with arrays to load, process and save data the way every Paper 4 file question expects. The three-step rhythm — open → read/write → close — is the backbone of every file task on the paper.

8.1 Opening and Closing a File

You cannot read a book that is still on the shelf — you have to take it down and open it first. A file works the same way. Before your program can touch a single character of a file, it must ask the operating system to open it, and it must say why it wants it (to read? to write?). Get this one line right and everything else in the chapter follows from it.

Python opens a file with the built-in open() function. It takes two things: the filename and the mode. It hands back a file handle — a variable you use for every later operation on that file.

file = open("data.txt", "r")   # filename, then mode
# ... read or write here ...
file.close()                       # always release the file

The mode is a one-letter string. There are exactly three you need for Paper 4, and the difference between them is the single most common source of lost marks in file questions:

ModeNameIf file does NOT existIf file DOES exist
"r"ReadCrashes (FileNotFoundError)Keeps contents — opens for reading only
"w"WriteCreates a new empty fileWipes contents instantly on open
"a"AppendCreates a new empty fileKeeps contents — new writes go at the end
  • "r" — read. The file must already exist. If it does not, Python crashes with FileNotFoundError. Why? Because there is nothing to read.
  • "w" — write. Creates the file if it does not exist. If it does exist, all existing content is wiped instantly, the moment the file opens — before you write anything.
  • "a" — append. Creates the file if needed, but if it exists, the existing content is kept and new writes go on the end. Logs, registers and records grow — you add to them, you do not retype them.
Key rule — w destroys, a preserves:
  • Opening an existing file in "w" mode erases everything in it immediately.
  • If the question says "add to the file" or "keep the existing data", you must use "a".
  • Choosing the wrong one is a logic error the examiner will spot at once.

And why does close() matter? Two reasons. First, written data is often held in a memory buffer and only physically saved to disk when the file closes — forget to close and your file can end up empty. Second, an open file is locked: other programs (or another part of your own program) may not be able to use it. Open, do the job, close. Every time.

Examiner focus — mark scheme:
  • In real Paper 4 mark schemes, "opening the file in the correct mode" and "closing the file in an appropriate place" are each worth their own mark — see 9618/42 M/J 2024 Q1(a) and 9618/42 O/N 2022 Q2(d).
  • Two marks are sitting there for two short lines of code.
  • Never leave them out.
Exam tip — use the parameter filename:
  • When the question gives you the filename as a parameter (e.g. def ReadData(filename):), use the parameter in open(filename, "r") — do not hard-code "data.txt".
  • Mark schemes specifically award "opening file using parameter filename".

8.2 Writing to a File

Every useful system you have ever used saves something — your game saves progress, your school saves your marks, your phone saves messages. Writing to a file is how a program turns its temporary results into a permanent record. This is also the easiest mode to start with, because there is nothing to read: you create the file yourself.

Open the file in "w" mode and call write() on the handle. One detail trips everyone up: write() does not move to a new line by itself, the way print() does. If you want each item on its own line — and in Paper 4 you almost always do — you must add the newline character "\n" yourself.

file = open("greeting.txt", "w")   # creates the file (or wipes it)
file.write("Hello, world!\n")         # \n ends the line
file.write("Welcome to Paper 4.\n")
file.close()                            # data is now safely on disk

The file greeting.txt will contain:

Hello, world!
Welcome to Paper 4.

One more rule that costs marks: write() accepts strings only. If you try file.write(75) Python crashes with a TypeError, because a file is a sequence of characters and 75 is a number, not characters. Convert first: file.write(str(75) + "\\n").

Key rule — write() takes strings only, and never adds \\n for you: Numbers must be wrapped in str(), and every line must end with + "\\n" if you want the next write to start on a new line.
Task — Worked Example — register.txt (3 names)
Write a Python program that creates a text file called register.txt and writes the names of three students — Musarrat, Tarnima and Aymaan — into it, one name per line.
Hint:
  • Choose the mode — we are creating a brand-new file, so open in "w" mode
  • Write each name with \n — three write() calls, each ending in "\n"
  • Close the file — this flushes the buffer so the names are physically saved
Your Turn — Your Turn — subjects.txt (3 subjects)
Write a Python program that creates a text file called subjects.txt and writes the names of three subjects — Physics, Chemistry and Computer Science — into it, one subject per line.
Hint:
  • Identical structure to the worked example: open in "w"
  • Three write() calls each ending in "\n"
  • Then close()
Task — Worked Example 7.2B — scores.txt (numbers)
Create scores.txt and write the numbers 85, 62 and 74 into it, one per line.
Hint:
  • Numbers must be converted with str() before writing
  • write(str(number) + "\n") for each value
  • Open in "w" mode and close at the end
Your Turn — Your Turn 7.2B — prices.txt
Create prices.txt and write the numbers 120, 55 and 300 into it, one per line.
Hint:
  • Use write(str(number) + "\n") for each value
  • Open prices.txt in "w" mode
  • Close the file at the end
Task — Worked Example 7.2C — user.txt (from input)
Ask the user for their name and save it into user.txt.
Hint:
  • input() already returns a string, so no str() is needed
  • Just add "\n" so any future append starts on a new line
  • Open in "w" mode and close at the end
Your Turn — Your Turn 7.2C — food.txt
Ask the user for their favourite food and save it into food.txt.
Hint:
  • Same three lines; only the prompt and filename change
  • input() returns a string — add "\n" when writing
  • Open food.txt in "w" mode
Exam tip:
  • In Paper 4, writing output to a file is usually the final part of a question and is marked per operation: open in the correct mode, write the correct data with a newline, close the file.
  • Examiners accept file.write(str(n) + "\\n") or an f-string like file.write(f'{n}\n') — both earn the mark.

8.3 Reading from a File

This is the skill Paper 4 tests most. The exam gives you a ready-made data file — names, scores, stock records — and your very first job is to get that data out of the file and into your program. If you can read a file line by line confidently, you can start every file question on the paper.

There are two ways to read, and you should know when to use each:

1. Read everything at once with read(). This returns the whole file as one big string. It is fine for simply displaying a file, but useless when you need to process items one at a time.

file = open("register.txt", "r")
content = file.read()   # the entire file as ONE string
print(content)
file.close()

2. Loop line by line. This is the Paper 4 way. Python lets you put the file handle straight into a for loop — each pass of the loop gives you the next line, and the loop stops automatically at the end of the file (EOF). No counting, no checking — Python handles EOF for you.

file = open("register.txt", "r")
for line in file:                # one line per loop pass, stops at EOF
    print(line.strip())          # .strip() removes the invisible \n
file.close()

Output (assuming register.txt contains Musarrat, Tarnima, Aymaan):

Musarrat
Tarnima
Aymaan

Why .strip()? Remember that every line in the file ends with an invisible \n. When Python reads the line, that \n comes along with it. If you print without stripping, you get the line's own \n plus print's newline — double spacing. Worse, if you compare line == "Musarrat" it will be False, because the line is actually "Musarrat\n". Strip first, always.

Key rule — .strip() always:
  • Every line you read carries a hidden \n — remove it with .strip() before you compare, convert or store it.
  • int("85\n") happens to work, but "Musarrat\n" == "Musarrat" is False, and stored lines with stray newlines corrupt your output later.
  • Make line.strip() a reflex.
Task — Worked Example — register.txt: "is present"
The file register.txt contains one student name per line. Write a Python program that reads the file and prints each name with the message " is present" after it.
Hint:
  • Open in "r" mode — we are only reading, and the file already exists
  • Loop line by line — for line in file: hands us each line and stops at EOF
  • Strip before using — without .strip() the output would be split across two lines
Your Turn — Your Turn — subjects.txt: "exam on Monday"
The file subjects.txt contains one subject per line. Write a Python program that reads the file and prints each subject with the message " exam on Monday" after it.
Hint:
  • Same four-line pattern: open "r"
  • for line in file:
  • strip
  • print with the message joined on
Task — Worked Example 7.3B — words longer than 5 letters
words.txt contains one word per line: apple banana kiwi grapefruit Print only the words longer than 5 letters.
Hint:
  • Strip first, then test len(word)
  • Testing len(line) would count the hidden \n too
  • Use if len(word) > 5: print(word)
Your Turn — Your Turn 7.3B — animals with more than 4 letters
animals.txt contains one animal name per line. Print only the names with more than 4 letters.
Hint:
  • Strip each line first
  • Use len(name) > 4 inside the loop
  • print(name) when the test is true
Task — Worked Example 7.3C — count the lines in scores.txt
scores.txt contains one mark per line. Count how many lines the file contains.
Hint:
  • Initialise count = 0 before the loop
  • Add 1 per line inside the loop
  • You do not even need .strip() just to count
Your Turn — Your Turn 7.3C — count the students in register.txt
register.txt contains one name per line. Count and print how many students are in the register.
Hint:
  • Identical counter pattern
  • You do not even need .strip() just to count
  • count = 0 before the loop, count = count + 1 inside
Exam tip — two habits, two marks:
  • Examiners award a mark for "removing the carriage return / newline from each line read in" — that is the .strip() mark.
  • They also award a mark for the loop itself reading until EOF; in Python, for line in file: earns it automatically.
  • Two marks for two habits.

8.4 Appending to a File

Think about a school attendance log. Each day you add today's record — you do not retype the whole year. That is exactly what append mode is for: keep everything already in the file and add new lines to the end. If you used "w" instead, day 2 would erase day 1. Real systems — logs, registers, transaction histories — live and die by this distinction.

Open in "a" mode. Everything else — write(), str(), "\n", close() — works exactly as in section 8.2. The only change is where the new text lands: at the end of the existing content instead of replacing it.

file = open("register.txt", "a")   # existing names are KEPT
file.write("Mehrin\n")                # added after the last line
file.close()

If register.txt previously contained Musarrat, Tarnima, Aymaan, after running this it contains:

Musarrat
Tarnima
Aymaan
Mehrin
Task — Worked Example — append a student name
The file register.txt already contains student names. Write a program that asks the user for a new student's name and adds it to the end of the file without losing the existing names.
Hint:
  • Read the requirement: "adds to the end... without losing" → append mode
  • "w" would be wrong here — it would lose marks and the data
  • input() gives a string; add "\n" so the next append starts on a fresh line
Your Turn — Your Turn — append a subject
The file subjects.txt already contains subject names. Write a program that asks the user for a new subject and adds it to the end of the file, keeping the existing subjects.
Hint:
  • Three lines after the input()
  • open with "a"
  • write with "\n", close
Task — Worked Example 7.4B — append the score 91
Append the score 91 to scores.txt.
Hint:
  • Open scores.txt in "a" mode
  • write(str(91) + "\n")
  • close()
Your Turn — Your Turn 7.4B — append the price 250
Append the price 250 to prices.txt.
Hint:
  • Open "a" mode
  • write(str(250) + "\n")
  • close()
Exam tip — read the verb in the question stem:
  • The verbs "add", "record" or "keep existing" always mean append.
  • The verbs "create" or "output the results to a new file" mean write.
  • The mode mark is awarded or lost on this single word.

8.5 The with Statement

Forgetting close() is the most common file bug there is — and it can silently leave your file empty or locked. Python gives you a pattern that makes the mistake impossible: with opens the file and guarantees it closes when the indented block ends, even if the program crashes mid-way. Professional Python code uses it everywhere, and it is fully accepted in Paper 4.

with open("register.txt", "r") as file:   # opens, names the handle "file"
    for line in file:
        print(line.strip())
# the block has ended -- the file is ALREADY closed here

Read it as a sentence: "with this file open as file, do the following." Everything indented under the with line happens while the file is open. The instant the indentation ends, Python closes the file for you — no close() call, no way to forget it.

Key rule — both styles are correct in Paper 4:
  • open()…close() and with open()… earn the same marks — real mark schemes note the close "might be within the Python opening file statement", meaning the with version.
  • Pick one style and use it consistently; just never write open() without a matching close().
Task — Worked Example — rewrite open/close to with
Rewrite the following program using the with statement. file = open("register.txt", "r") content = file.read() print(content) file.close()
Hint:
  • Merge open() into the with line: with open("register.txt", "r") as file:
  • Indent the reading and printing inside the block
  • Delete close() — it is no longer needed
Your Turn — Your Turn — rewrite log.txt append to with
Rewrite this program using the with statement. file = open("log.txt", "a") file.write("Backup completed\n") file.close()
Hint:
  • The mode "a" goes inside the with line exactly where it went inside open()
  • Indent the write() call inside the block
  • Delete close()
Exam tip — do not mix styles:
  • If a question gives you a partial program already using open()/close(), continue in that style — do not mix the two in one answer.
  • Consistency reads as confidence to an examiner; a with block that also calls close() reads as confusion.

8.6 Text Files + Arrays: Loading and Saving a List

A file on disk is slow and can only be read forwards, line by line. A list in memory is fast and lets you jump to any element, sort, search and count. So every serious Paper 4 program follows the same plan: read the file into a list once, do all the work on the list, write results back at the end. This section is the bridge between Chapter 4 (lists) and everything the exam will throw at you.

Loading a file into a list is the read loop from 8.3 plus one extra line: instead of printing each cleaned line, append() it to a list you created empty before the loop.

numbers = []                              # 1. start with an empty list
file = open("numbers.txt", "r")            # 2. open to read
for line in file:                          # 3. one line per pass, stops at EOF
    numbers.append(int(line.strip()))      # 4. clean, convert, store
file.close()                               # 5. release the file
print(numbers)                             # [10, 20, 30, 40, 50]

If numbers.txt contains 10, 20, 30, 40, 50, the output is:

[10, 20, 30, 40, 50]

Look closely at line 4 — it does three jobs, inside-out: line.strip() removes the newline, int(…) converts the text "10" into the number 10, and numbers.append(…) stores it. Why convert? Because a file can only hold characters. The file contains the two characters "1" and "0", not the number ten. Until you convert, "10" + "20" gives "1020" — string joining, not addition.

Key rule — everything read from a text file is a string:
  • Reading numbers? Convert with int() or float() as you load them.
  • Reading names? .strip() is enough.
  • Forgetting the conversion is the classic cause of "my total is 1020 instead of 30".
Examiner focus — the 5-step skeleton is the mark scheme:
  • This exact pattern — declare array, open file, loop to EOF, read each line, store in the array, close file — is the published mark scheme breakdown of 9618/42 M/J 2024 Q1(a), where each step listed is worth one mark.
  • Learn the five-step skeleton above and you can write it for any file the exam gives you.

Saving a list to a file is the mirror image: loop through the list and write each element with str() and "\n".

numbers = [10, 20, 30, 40, 50]
file = open("output.txt", "w")
for n in numbers:
    file.write(str(n) + "\n")
file.close()
Task — Worked Example — marks.txt into a list + total
The file marks.txt contains one test mark per line: 67 82 75 58 90 Write a program that reads the marks into a list, then prints the list and the total of all the marks.
Hint:
  • Load with the five-step skeleton: empty list, open "r", loop, strip + int + append, close
  • Work on the list, not the file — the file is already closed
  • Totalling is pure list work from Chapter 4
Your Turn — Your Turn — temps.txt into a list + average
The file temps.txt contains one temperature reading per line. Write a program that reads the readings into a list, then prints the list and the average of all readings.
Hint:
  • Load exactly as in the worked example
  • Then average = total / len(list)
  • Sum the list with the same total accumulator pattern
Task — Worked Example 7.6B — words.txt into a list
Read the words from words.txt into a list and print the list.
Hint:
  • Same 5-step skeleton but no int() this time
  • Names and words need .strip() only
  • append(line.strip()) inside the loop
Your Turn — Your Turn 7.6B — colors.txt into a list
Read the colour names from colors.txt into a list and print the list.
Hint:
  • .strip() removes the newline from each colour
  • No conversion needed — they stay as strings
  • Five-step skeleton with append(line.strip())
Task — Worked Example 7.6C — list to output.txt
The list [5, 10, 15, 20, 25] is given. Write each number to output.txt, one per line.
Hint:
  • The saving loop: str() out, just as int() came in
  • Open output.txt in "w" mode
  • Loop the list, write str(v) + "\n"
Your Turn — Your Turn 7.6C — team.txt names
The list ["Naib", "Mohar", "Nazifa"] is given. Write each name to team.txt, one per line.
Hint:
  • Names are already strings
  • write(name + "\n") — no str() needed
  • Open team.txt in "w" mode
Exam tip — known count vs unknown count:
  • When a question says the file contains a known number of items (e.g. "the file stores 10 records"), for i in range(10): with file.readline() is also accepted — mark schemes credit "looping until EOF / 10 times" as alternatives.
  • When the count is unknown, only the EOF loop works.

8.7 Processing File Data

Loading a file is never the whole question — it is the setup. The marks come from what you do next: count the values that meet a condition, split them into groups, find the largest, merge two sources. Each of these is a small pattern you already met in Chapters 3 and 4, now fed by file data instead of typed input. This section drills the four patterns the exam recycles again and again.

Pattern 1 — count with a condition

nums = []
file = open("numbers.txt", "r")
for line in file:
    nums.append(int(line.strip()))
file.close()

pos = 0
neg = 0
for n in nums:
    if n > 0:
        pos = pos + 1
    elif n < 0:
        neg = neg + 1

print("Positive:", pos)
print("Negative:", neg)

If numbers.txt contains -5, 12, -8, 0, 3, -1, the output is:

Positive: 2
Negative: 3

Notice that 0 is counted by neither branch — the elif chain deliberately lets it fall through. Always check what the question wants done with boundary values.

Pattern 2 — split into two lists

nums = []
file = open("numbers.txt", "r")
for line in file:
    nums.append(int(line.strip()))
file.close()

even = []
odd = []
for n in nums:
    if n % 2 == 0:
        even.append(n)
    else:
        odd.append(n)

print("Even:", even)
print("Odd:", odd)

Pattern 3 — file in, two files out

Combine pattern 2 with the saving loop from 8.6: after splitting, write each list to its own file.

nums = []
file = open("numbers.txt", "r")
for line in file:
    nums.append(int(line.strip()))
file.close()

evenFile = open("even.txt", "w")
oddFile = open("odd.txt", "w")
for n in nums:
    if n % 2 == 0:
        evenFile.write(str(n) + "\n")
    else:
        oddFile.write(str(n) + "\n")
evenFile.close()
oddFile.close()

Two files can be open at the same time — as long as each has its own handle and each gets its own close().

Pattern 4 — combine two files into one list

data1 = []
file1 = open("file1.txt", "r")
for line in file1:
    data1.append(int(line.strip()))
file1.close()

data2 = []
file2 = open("file2.txt", "r")
for line in file2:
    data2.append(int(line.strip()))
file2.close()

combined = data1 + data2   # + joins two lists end to end
print(combined)
Key rule — one job, one loop:
  • Load in one loop, process in another, save in a third.
  • Beginners try to read, test and write inside a single loop and tie themselves in knots.
  • Separate loops are easier to write, easier to trace, and match how mark schemes award the marks step by step.
Task — Worked Example — scores.txt → max/min → summary.txt
The file scores.txt contains one test score per line: 67 82 75 58 90 Write a program that reads the scores into a list, finds the highest and lowest score, and writes both results to a new file called summary.txt.
Hint:
  • Load — the five-step skeleton from 8.6 fills the list scores
  • Process — Python's built-ins max() and min() scan the list for us
  • Save — open summary.txt in "w" mode (the question says a NEW file)
  • Write both lines, converting numbers with str()
Your Turn — Your Turn — heights.txt → tallest/shortest → report.txt
The file heights.txt contains one student height (in cm) per line. Write a program that reads the heights into a list, finds the tallest and shortest height, and writes both results to a new file called report.txt.
Hint:
  • Identical three-phase structure
  • Load skeleton, then max() / min()
  • Write two lines to "report.txt" in "w" mode
Task — Worked Example 7.7B — count above/below 50
scores.txt contains one score per line. Count how many scores are above 50 and how many are below 50.
Hint:
  • Pattern 1 with a different condition
  • above = 0, below = 0 before the loop
  • if s > 50 → above; elif s < 50 → below
  • A score of exactly 50 is counted by neither — as the question implies
Your Turn — Your Turn 7.7B — ages 18 or over / under 18
ages.txt contains one age per line. Count how many ages are 18 or over, and how many are under 18.
Hint:
  • This time the two conditions cover everything
  • if age >= 18 … else …
  • adults = 0, minors = 0 before the loop
Task — Worked Example 7.7C — even/odd to two files
Read numbers from values.txt; write the even numbers to output_even.txt and the odd numbers to output_odd.txt.
Hint:
  • Pattern 3 — two output handles, two closes
  • evenFile and oddFile both opened in "w" mode
  • Test: if n % 2 == 0 → evenFile, else → oddFile
Your Turn — Your Turn 7.7C — distinction vs standard
Read marks from marks.txt; write marks of 75 and above to distinction.txt and all other marks to standard.txt.
Hint:
  • Same shape as 7.7C
  • The test becomes if m >= 75
  • Two output handles: dFile and sFile
Task — Worked Example 7.7D — combine two files into one
Read numbers from data1.txt and data2.txt, combine them into one list, and save the combined list to combined_data.txt.
Hint:
  • Pattern 4 plus the saving loop
  • Three files touched; each opened, used, closed
  • combined = data1 + data2
Your Turn — Your Turn 7.7D — combine grade11 + grade12
Read names from grade11.txt and grade12.txt, combine them into one list, and save the combined list to all_students.txt.
Hint:
  • Names need .strip() only
  • The combine and save steps are identical to 7.7D
  • combined = g11 + g12; write each name + "\n"
Exam tip — meaningful handle names:
  • In a multi-file program, give every handle a meaningful name (evenFile, out) instead of reusing file while another file is still open.
  • Reused handles are the top cause of &quot;file written to the wrong place&quot; errors under exam pressure — and they make your code harder for the examiner to follow.

8.8 Full Exam-Style Question

Everything above was drilled one skill at a time. The real paper hands you one scenario and makes you chain the skills: read a file into a global array, process the array, write results out. Attempt this under timed conditions (about 18 minutes) before opening the mark scheme.

Paper 4 · Section 20.2 · File Processing
  • A school stores the results of a Computer Science test in the text file results.txt.
  • Each line of the file contains one mark as an integer.
  • The number of lines in the file is unknown.

The program uses the global variables:

marks = []           # global 1D array of integers
numberMarks = 0     # global integer, the number of marks read

(a) ReadMarks(fileName) — [6 marks] AO3

Write the Python function ReadMarks(fileName) that takes the name of the file as a parameter, reads every mark from the file into the global array marks, stores the number of marks read in numberMarks, and returns numberMarks.

(b) CountDistinctions() — [3 marks] AO3

Write the Python function CountDistinctions() that returns the number of marks in the array that are greater than or equal to 75.

(c) SaveSummary() — [4 marks] AO3

Write the Python procedure SaveSummary() that writes two lines to a new text file summary.txt:

  • the first line contains the number of marks, in the form Total: 25
  • the second line contains the number of distinctions, in the form Distinctions: 7

Your procedure should call CountDistinctions().

(d) Why is "w" wrong? — [1 mark] AO1

State one reason why opening results.txt in "w" mode in part (a) would be incorrect.

Total: 14 marks

Task — Model solutions + mark scheme
Click "Show Answer" to reveal the model solutions for parts (a)–(d) and the full Cambridge-style mark scheme.
Hint:
  • Part (a) is the 5-step skeleton plus the "global" twist
  • Part (b) — use >= 75 (not > 75)
  • Part (c) — open in "w" mode (the stem says "a new file")
  • Part (d) — any ONE reason: "w" wipes the file / "w" opens for writing so marks cannot be read
Exam tip — the global keyword:
  • Part (a) is six marks for the five-step skeleton you drilled in 8.6 plus one twist: global.
  • When a Paper 4 question says the array and counter are global, you must write global marks, numberMarks as the first line inside the function before assigning to them — omitting it makes numberMarks = numberMarks + 1 crash in Python, and the mark scheme expects it.

Key Points Summary

open(filename, mode) returns a file handle. Always close() it when done — written data sits in a memory buffer until close() flushes it to disk.
Three modes: "r" = read (file must exist), "w" = write (wipes the file on open), "a" = append (keeps existing content, writes at the end).
"w" destroys, "a" preserves. Choose the wrong mode and you lose both the mark and the data — read the verb in the question stem ("add" → append, "create" → write).
write() accepts strings only. Wrap numbers in str() and add "\n" yourself: file.write(str(n) + "\n"). write() does NOT add a newline automatically.
for line in file: loops line by line and stops automatically at EOF (end of file). No counting, no checking — Python handles it for you.
.strip() removes the hidden \n from each line you read. Make line.strip() a reflex — it is its own mark on the mark scheme.
read() returns the WHOLE file as one big string. Use it for displaying; use the line-by-line loop for processing items one at a time.
The with statement — with open(filename, mode) as file: — closes the file for you automatically when the indented block ends, even if the program crashes. Both open()/close() and with are accepted.
Never mix styles: if a partial program uses open()/close(), continue in that style. Do not call close() inside a with block.
The 5-step skeleton to load a file into a list: (1) declare empty list, (2) open in "r" mode, (3) loop to EOF, (4) strip + int + append each line, (5) close. Each step is its own mark in 9618/42 M/J 2024 Q1(a).
Everything read from a text file is a string. Convert numbers with int() (or float()) as you load them, otherwise "10" + "20" gives "1020" not 30.
Saving a list back to a file is the mirror image: loop, str() each element, write with "\n", close.
One job, one loop: load in one loop, process in another, save in a third. Separate loops are easier to write, trace and mark.
Two files can be open at the same time, as long as each has its own handle (e.g. evenFile, oddFile) and each gets its own close().
list1 + list2 joins two lists end to end — used for the "combine two files into one list" pattern.
When a question says the array and counter are global, write "global marks, numberMarks" as the first line inside the function — otherwise reassigning the global crashes with UnboundLocalError.
Boundary values: "75 or more" means m >= 75 (not m > 75). Always check whether the question says "above", "at least" or "or above".
A question that gives the filename as a parameter (e.g. def ReadData(filename):) expects you to use open(filename, "r") — never hard-code "data.txt".

8.9 Practice Tasks

Fifteen exam-style file-handling tasks. Each has a Hint (thought process, bullet points) and a Help button (full Python solution). Try the task first, use the Hint if you are stuck, then check the Help to verify.

1Practice Task — Create and write a register [3 marks]
Write a Python program that creates a text file class_register.txt and writes the names Alice, Bob and Charlie into it, one name per line.
2Practice Task — Write integers to a file [3 marks]
Write a Python program that creates a file data.txt and writes the integers 7, 14, 21, 28 into it, one per line.
3Practice Task — Read a file and print each line with .strip() [3 marks]
Write a Python program that opens names.txt in read mode and prints each name on its own line, without any extra blank lines between them.
4Practice Task — Print only lines longer than 6 characters [4 marks]
A file cities.txt contains one city name per line. Write a Python program that prints only the city names that are longer than 6 characters.
5Practice Task — Append without destroying [3 marks]
A file log.txt already exists with several entries. Write a Python program that asks the user for a new entry and adds it to the end of the file without losing the existing entries.
6Practice Task — Append a number — str() still needed [2 marks]
Append the integer 250 to a file prices.txt that already contains other prices.
7Practice Task — Rewrite open/close using with [3 marks]
Rewrite this code using the with statement so that close() is no longer needed: file = open("config.txt", "r") setting = file.read() print(setting) file.close()
8Practice Task — Load integers into a list — 5-step skeleton [5 marks]
Write a Python program that reads every integer from nums.txt into a list called nums using the 5-step skeleton, then prints the list.
9Practice Task — Load + total + average [5 marks]
A file marks.txt contains one integer mark per line. Read the marks into a list, then print the total of the marks and the average.
10Practice Task — Load + count passing scores [4 marks]
A file scores.txt contains one integer score per line. Read the scores into a list, then print how many scores are 50 or above (the pass mark).
11Practice Task — Split into two lists — even and odd [5 marks]
A file numbers.txt contains one integer per line. Read the numbers into a list, then split them into two lists: evens and odds. Print both lists.
12Practice Task — File in, two files out (Pattern 3) [6 marks]
Read integers from input.txt. Write the even integers to even.txt and the odd integers to odd.txt, one per line in each file.
13Practice Task — Find max and min, write to summary [5 marks]
A file temps.txt contains one integer temperature reading per line. Read the temperatures into a list, find the highest and lowest, and write them to a new file extreme_temps.txt in the form "Highest: X" and "Lowest: Y".
14Practice Task — Combine two files into one (Pattern 4) [6 marks]
Read integers from data1.txt and data2.txt, combine them into a single list, and write the combined list to combined.txt, one per line.
15Practice Task — Full exam-style — ReadMarks + CountDistinctions [9 marks]
A file results.txt contains one integer mark per line. The global variables marks = [] and numberMarks = 0 are declared. Write: (a) function ReadMarks(fileName) that reads every mark into marks, stores the count in numberMarks, and returns numberMarks [6] (b) function CountDistinctions() that returns the number of marks that are 75 or more [3]

Question Bank

Answer all questions, then press Submit Quiz to see your score.

0/12 answered

Question 1Multiple Choice

What is the correct way to open data.txt for reading?

Question 2Multiple Choice

You call file.write(50) on a freshly opened file. What happens?

Question 3True / False

Calling file.write("Hello") automatically moves the cursor to the next line.

Question 4Multiple Choice

A file register.txt contains the name "Musarrat" on its own line. You read it with for line in file: and compare line == "Musarrat". What is the result?

Question 5Multiple Choice

A question stem says: "Add the new sale to the end of sales.txt, keeping the existing sales." Which mode and which conversion are needed?

Question 6True / False

The with statement closes the file for you automatically when the indented block ends, even if the program crashes mid-way.

Question 7Multiple Choice

Which snippet correctly loads a file of integers into a list using the 5-step skeleton?

Question 8Multiple Choice

A file temps.txt contains 5 temperature readings. Which code finds the average?

Question 9True / False

In a program that writes even numbers to even.txt and odd numbers to odd.txt, you can keep both files open at the same time using two separate handles.

Question 10Multiple Choice

A question says "create a NEW file summary.txt and write the total marks to it". Which mode is correct?

Question 11True / False

In CountDistinctions() the test m > 75 is correct because the question says "75 or more".

Question 12Multiple Choice

You have data1 = [1, 2, 3] and data2 = [4, 5, 6] loaded from two files. What does data1 + data2 give?

Answer all 12 questions to enable submission.