Editor

Python Program to Update Marks in a Binary File

Intermediate · File handling · CBSE class 12 practical list

This program creates a binary file of student records (roll number, name and marks) with the pickle module, then changes the marks of the student with a given roll number. The safe way to do it is to read every record, change the one that matches and write them all back. It is a standard CBSE class 12 practical program.

Program

binary_file_update.py
import pickle
# Make a sample file to work on (in your practical file, this file already exists)
with open("students.dat", "wb") as f:
pickle.dump([101, "Aarav", 78], f) # each record is [roll number, name, marks]
pickle.dump([102, "Diya", 85], f)
pickle.dump([103, "Kabir", 64], f)
def read_all():
records = []
with open("students.dat", "rb") as f:
try:
while True:
records.append(pickle.load(f))
except EOFError: # no more records
pass
return records
def update_marks(roll, new_marks):
records = read_all()
for record in records:
if record[0] == roll:
print("Changing marks of", record[1], "from", record[2], "to", new_marks)
record[2] = new_marks
with open("students.dat", "wb") as f: # write every record back
for r in records:
pickle.dump(r, f)
return
print("Roll number", roll, "not found, nothing updated")
print("Before:", read_all())
update_marks(103, 72) # or: update_marks(int(input("Roll: ")), int(input("New marks: ")))
update_marks(110, 90)
print("After:", read_all())
Output
Before: [[101, 'Aarav', 78], [102, 'Diya', 85], [103, 'Kabir', 64]]
Changing marks of Kabir from 64 to 72
Roll number 110 not found, nothing updated
After: [[101, 'Aarav', 78], [102, 'Diya', 85], [103, 'Kabir', 72]]

How it works

  1. The program writes three records to students.dat with pickle.dump, one record per call.
  2. read_all() loads the records one by one until EOFError says the file has ended, and returns them as a list.
  3. update_marks() looks for the roll number in that list and changes the marks of the matching record with record[2] = new_marks.
  4. Opening the file again with "wb" empties it, and the loop dumps every record back, now with the new marks.
  5. return ends the function after a successful update, so the not found message is printed only when no record matched, as for roll number 110.

Another way to write it

Updating the record in place with seek()

Updating the record in place with seek()
import pickle
# Make a sample file to work on (in your practical file, this file already exists)
with open("students.dat", "wb") as f:
for record in [[101, "Aarav", 78], [102, "Diya", 85], [103, "Kabir", 64]]:
pickle.dump(record, f)
roll, new_marks = 102, 91
with open("students.dat", "rb+") as f: # read and write the same file
try:
while True:
position = f.tell() # where this record starts
record = pickle.load(f)
if record[0] == roll:
record[2] = new_marks
f.seek(position) # go back to the start of the record
pickle.dump(record, f) # and write over it
print("Updated:", record)
break
except EOFError:
print("Roll number", roll, "not found")
Output
Updated: [102, 'Diya', 91]

Many textbooks use this "rb+" version. It overwrites the record where it sits, which is safe only when the new record takes exactly as many bytes as the old one: marks from 0 to 255 always do, but a longer name would damage the records after it. Rewriting the whole file, as in the main program, is safe for any change.

Try it yourself

Change the program to add 5 bonus marks to every student instead of updating just one.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises