Question 422 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()

``

Answer keywords:pickle.dump()binary file"ab" modetuple recordR_IDR_NameR_ExpertiseCharges

Marking scheme

  1. 1.1 mark: opening RESOURCES.DAT in correct binary-append mode ("ab") and importing pickle.
  2. 2.1 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.

Common mistakes

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

Analysis and 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.

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.

Question details

Q42 of 48 questions

Board
CBSE
Class
Class 12
Subject
Computer Science
Year
2026
Section
Section E
Marks
2
Type
Long Answer
Chapter
File Handling in Python
Topic
Binary File Handling — Writing Records with pickle
Bloom's level
apply
Syllabus topic
Unit 1: Computational Thinking and Programming-2 — Binary File Handling: writing tuples using the pickle module (dump)
Author
CBSE
Reviewed by
Padhantu Academic Team

Spotted a mistake or something unclear?

Tell us and we will fix it.

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.