LambdaLabTM
Computer Science · Class 12 · Binary Files
Binary filesAppending⏱️ 10 min read

Appending Records

The shortest lesson in the chapter, and the one with the most expensive mistake in it. Adding a record to a file that already has records is the create program with one letter changed.

1'wb' replaces. 'ab' adds.

open('student.dat', 'wb')

The file is emptied the moment it is opened. Whatever was saved before this run is gone, and nothing can bring it back.

open('student.dat', 'ab')

The pointer starts at the end of the file, so every dump() lands after the records already there. Nothing can be lost.

append_record.py
import pickle

roll = int(input('Roll number : '))
name = input('Name        : ')
marks = int(input('Marks       : '))

f = open('student.dat', 'ab')
pickle.dump([roll, name, marks], f)
f.close()

print('Record added.')

f = open('student.dat', 'rb')
try:
    while True:
        print(pickle.load(f))
except EOFError:
    pass
f.close()
Output
Roll number : 4
Name        : Neha
Marks       : 88
Record added.
[1, 'Ravi', 78]
[2, 'Meera', 91]
[3, 'Amit', 65]
[4, 'Neha', 88]
open('student.dat', 'ab')

Append, binary. The three records already in the file are untouched.

pickle.dump([roll, name, marks], f)

Exactly the same call as when the file was created. Only the mode decided where it landed.

open('student.dat', 'rb')

A second handle, to show the result. The appending handle was closed first, so everything it wrote is on the disk.

Run it twice and look
Run an appending program twice and the file has two new records. Run a 'wb' program twice and it still has one — the second run threw the first one away. This is the difference the exam asks about, and the difference that loses real data.

2'ab' also creates the file

If student.dat does not exist yet, 'ab' makes it, exactly as 'wb' would. So an appending program is safe to run the very first time — there is no need to create the file separately, and no FileNotFoundError to catch.

Which means 'ab' is usually the better default
A program that adds today's records should almost always open the file in 'ab'. It works on the first run and on every run after it, and it cannot destroy what is already saved. 'wb' is for the one program whose job really is to start the file again.

3Appending several records

Same loop as the create program — the only change is the mode on the open() line:

append_many.py
import pickle

more_students = [
    [5, 'Kabir', 82],
    [6, 'Sara', 74],
]

f = open('student.dat', 'ab')
for s in more_students:
    pickle.dump(s, f)
f.close()

print('Added', len(more_students), 'records.')
Output
Added 2 records.

4Try it

Press Run twice and watch the record count grow. Then change 'ab' to 'wb' and press Run twice again:

appending.py

5Recap

'ab' adds, 'wb' replaces

The only difference between the append program and the create program.

The pointer starts at the end

Which is why an append can never overwrite a record already in the file.

dump() is unchanged

The same call writes the record. The mode alone decided where it went.

'ab' creates a missing file

So an appending program works on the first run as well as the hundredth.

Close before reading back

Then open a fresh handle in 'rb' to display the file.

Prefer 'ab' in real programs

'wb' is for the one program that is meant to start the file again from empty.

✍️ Now write these yourself
  1. 1

    Write a program that keeps asking for records and appends each one, until the user says no.

    Hint · The create loop, with 'ab' instead of 'wb'.

  2. 2

    Run it three times and count the records afterwards.

    Hint · The count grows every run. That is the whole test.

  3. 3

    Delete student.dat, then run your appending program.

    Hint · It works. 'ab' creates the file.

  4. 4

    Change one 'ab' to 'wb' and run it twice, then count.

    Hint · One record. Change it back.

Quick Check

Which mode adds a record without touching the ones already in the file?

Quick Check

An append program is run four times, one record each time. How many records are in the file?

Quick Check

student.dat does not exist. What does open('student.dat', 'ab') do?