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.
The file is emptied the moment it is opened. Whatever was saved before this run is gone, and nothing can bring it back.
The pointer starts at the end of the file, so every dump() lands after the records already there. Nothing can be lost.
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()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.
'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.
'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:
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.')Added 2 records.
4Try it
Press Run twice and watch the record count grow. Then change 'ab' to 'wb' and press Run twice again:
5Recap
The only difference between the append program and the create program.
Which is why an append can never overwrite a record already in the file.
The same call writes the record. The mode alone decided where it went.
So an appending program works on the first run as well as the hundredth.
Then open a fresh handle in 'rb' to display the file.
'wb' is for the one program that is meant to start the file again from empty.
- 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
Run it three times and count the records afterwards.
Hint · The count grows every run. That is the whole test.
- 3
Delete
student.dat, then run your appending program.Hint · It works. 'ab' creates the file.
- 4
Change one
'ab'to'wb'and run it twice, then count.Hint · One record. Change it back.
Which mode adds a record without touching the ones already in the file?
An append program is run four times, one record each time. How many records are in the file?
student.dat does not exist. What does open('student.dat', 'ab') do?