Q42
2 marksLong AnswerSection E

Append() - To input the data of a Resource Person and write it in the file RESOURCES.DAT.

File Handling in Python
Binary File Handling — Writing Records with pickle
Official Answer

The Append() function must open RESOURCES.DAT in append-binary mode, take user input for each field, pack it into a tuple matching the record structure, and pickle-dump it into the file.


``python

import pickle


def Append():

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

R_ID = int(input("Enter Resource ID: "))

R_Name = input("Enter Resource Name: ")

R_Expertise = input("Enter Area of Expertise: ")

Charges = float(input("Enter Charges per hour: "))

rec = (RID, RName, R_Expertise, Charges)

pickle.dump(rec, f)

f.close()

``

pickle.dump()binary file"ab" modetuple recordR_IDR_NameR_ExpertiseCharges

Marking Scheme

  • 11 mark: opening RESOURCES.DAT in correct binary-append mode ("ab") and importing pickle.
  • 21 mark: correctly taking input for all four fields, forming the tuple in the exact order (R_ID, R_Name, R_Expertise, Charges), and using pickle.dump() to write it.

Hint

Open the file in "ab" mode, take inputs for R_ID, R_Name, R_Expertise, Charges, pack them into a tuple, and pickle.dump() it.

Quick Oral Answer

Append() opens RESOURCES.DAT in "ab" mode, accepts the four field values from the user, forms a tuple in the given record order, and writes it using pickle.dump() so existing records are preserved.

Analysis & Explanation

This is a standard binary-file-write question built around Python's pickle module.


Concept:

  • Mode "ab" opens the file for appending in binary form without erasing existing records.
  • pickle.dump(object, file) serialises the Python tuple directly, preserving its data types (int, str, float) exactly as given in the record structure.

Exam trap: using mode "a" (text) instead of "ab" (binary) loses marks because RESOURCES.DAT is explicitly a binary file; forgetting import pickle is another common slip.


Real-world application: appending pickled tuples one record at a time is exactly how lightweight local databases (e.g. contact lists, small inventory apps) persist structured data without needing a full SQL server.

Common Mistakes

  1. 1Opening the file in text mode ("a") instead of binary append mode ("ab").
  2. 2Forgetting to import the pickle module before calling pickle.dump().
  3. 3Writing the fields as separate dump() calls instead of one tuple matching the given record structure.

Interesting Facts

pickle can serialise almost any native Python object (tuples, lists, dictionaries, even class instances), which is why CBSE binary file questions consistently use it over manual byte encoding.

Files opened in "ab" mode always write at the end of the file regardless of where the file pointer was moved earlier in the program.

Because pickle stores type information along with the data, reading it back with pickle.load() automatically restores the original tuple with the same int/str/float types — no manual type casting is needed.

Spotted a mistake or something unclear?

Tell us — we fix reported answers fast.

Frequently Asked Questions

Why must RESOURCES.DAT be opened in "ab" and not "wb" mode?

"wb" mode truncates (erases) the file before writing, destroying all previously stored resource person records; "ab" mode preserves existing data and adds the new record at the end.

Can Append() write multiple records in one call?

The base version writes one record per call, but it is commonly extended with a while loop and a 'more records?' prompt to add several records in one run — either version earns full marks.

What error occurs if pickle is not imported?

Python raises a NameError: name 'pickle' is not defined as soon as pickle.dump() is called, since the module must be imported before use.