Q43
3 marksLong AnswerSection E

Update() - To increase the Charges of each resource person by 500.

File Handling in Python
Binary File Handling — Updating Records
Official Answer

Since tuples are immutable, Update() cannot change Charges in place; it must read every record into a list, rebuild each tuple with the increased Charges, and rewrite the whole file.


``python

import pickle


def Update():

f = open("RESOURCES.DAT", "rb")

records = []

try:

while True:

rec = pickle.load(f)

records.append(rec)

except EOFError:

pass

f.close()


f = open("RESOURCES.DAT", "wb")

for RID, RName, R_Expertise, Charges in records:

Charges = Charges + 500

newrec = (RID, RName, RExpertise, Charges)

pickle.dump(new_rec, f)

f.close()

``


Result: every resource person's Charges field in RESOURCES.DAT is permanently increased by 500 rupees.

pickle.load()EOFErrortuple immutability"rb" mode"wb" modeCharges += 500rewrite binary file

Marking Scheme

  • 11 mark: correctly reading all existing records using pickle.load() in a loop terminated by EOFError.
  • 21 mark: correctly recreating each tuple with Charges increased by 500 (recognising tuple immutability).
  • 31 mark: reopening RESOURCES.DAT in "wb" mode and writing back every updated record with pickle.dump().

Hint

Read all records into a list using pickle.load() inside a try/except EOFError loop, add 500 to Charges while rebuilding each tuple, then reopen the file in "wb" mode and dump the updated list.

Quick Oral Answer

Update() reads every record from RESOURCES.DAT into a list using pickle.load() until EOFError, adds 500 to each record's Charges while rebuilding the tuple, then reopens the file in "wb" mode and writes all the updated tuples back.

Analysis & Explanation

This question checks whether students understand that binary records stored as tuples cannot be edited in place.


Concept:

  • Read phase: open in "rb" mode and repeatedly pickle.load() until an EOFError signals end of file, collecting every record in a list.
  • Modify phase: unpack each tuple, add 500 to Charges, and rebuild a new tuple (tuples cannot be mutated directly).
  • Write phase: reopen the same filename in "wb" mode (which safely overwrites) and dump all the updated records back.

Exam trap: many students try to "update" the file while still reading it, or forget the try...except EOFError block, causing the read loop to crash or run forever; others attempt rec[3] += 500 directly, which fails because tuples are immutable.


Real-world application: this two-pass read-modify-rewrite pattern is exactly how any program without random-access database support performs a bulk update, such as a payroll script applying an across-the-board salary hike.

Common Mistakes

  1. 1Trying to modify a tuple element directly (e.g. rec[3] += 500), which raises a TypeError since tuples are immutable.
  2. 2Omitting the try/except EOFError block, so the reading loop either crashes or never terminates.
  3. 3Reading and writing the file simultaneously in the same open() handle instead of doing a full read pass followed by a full rewrite pass.

Interesting Facts

EOFError is Python's built-in signal that pickle.load() has reached the end of the file — there is no separate 'end of file' marker stored in the file itself.

Opening a file in "wb" mode on an existing filename silently truncates it to zero bytes before writing, which is exactly the behaviour this function relies on to safely replace all records.

Because the record structure uses a tuple (not a list or dictionary), any update to CBSE binary-file questions like this one always requires the read-all, modify, rewrite-all pattern rather than an in-place edit.

Spotted a mistake or something unclear?

Tell us — we fix reported answers fast.

Frequently Asked Questions

Why can't Charges be increased directly inside the tuple?

Tuples in Python are immutable, so an expression like rec[3] += 500 raises a TypeError; a brand-new tuple with the updated value must be created instead.

Why is the file opened twice — once in "rb" and once in "wb"?

The first "rb" open reads all existing records into memory; the file must then be closed and reopened in "wb" mode, which truncates it, so the modified records can be written back cleanly without mixing old and new data.

What if the file has zero records?

The read loop immediately hits EOFError and records stays an empty list; the write loop then simply does nothing, leaving an empty (but valid) RESOURCES.DAT file.