Skip to content
Question

Q.(a) Write a point of difference between append

(a) and write (w) modes in a text file. Write a program in Python that defines and calls the following user defined functions :
(i) Add_Teacher() : It accepts the values from the user and inserts record of a teacher to a csv file 'Teacher.csv'. Each record consists of a list with field elements as T_id, Tname and desig to store teacher ID, teacher name and designation respectively.
(ii) Search_Teacher() : To display the records of all the PGT (designation) teachers.
(OR)
(b) Write one point of difference between seek() and tell() functions in file handling. Write a program in Python that defines and calls the following user defined functions :
(i) Add_Device() : The function accepts and adds records of the peripheral devices to a csv file 'peripheral.csv'. Each record consists of a list with field elements as P_id, P_name and Price to store peripheral device ID, device name, and price respectively.
(ii) Count_Device() : To count and display number of peripheral devices, whose price is less than Rs. 1000.
CBSECBSE Class XII Board 2023Subjective· 5mImportance★★★★★
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Part (a): 'w' erases existing content, 'a' keeps it and adds at the end; Add_Teacher() appends a teacher record, Search_Teacher() lists all PGT teachers.

Part (b): tell() returns the pointer position, seek() moves it; Add_Device() appends a device record, Count_Device() counts devices priced under Rs. 1000.

Part (a)

append vs write. Both modes create the file if it is absent. The decisive difference is what they do to an existing file:

  • 'w' (write): the file is truncated — all previous content is deleted — and writing starts from the beginning. Use it to create fresh / overwrite.
  • 'a' (append): the previous content is preserved and the file pointer is placed at the end, so new data is added after the old data. Use it to keep adding records (e.g. logs, new rows).

Because we keep adding teacher records, Add_Teacher() opens the CSV in 'a' mode. Search_Teacher() reads the file and prints rows whose third field (designation) equals PGT.

import csv

def Add_Teacher():
    with open('Teacher.csv', 'a', newline='') as f:      # append: keeps old records
        w = csv.writer(f)
        t_id  = input("Enter Teacher ID: ")
        tname = input("Enter Teacher Name: ")
        desig = input("Enter Designation: ")
        w.writerow([t_id, tname, desig])

def Search_Teacher():
    with open('Teacher.csv', 'r', newline='') as f:
        for row in csv.reader(f):
            if len(row) >= 3 and row[2].upper() == 'PGT':
                print(row)

Add_Teacher()
Search_Teacher()

If the file already holds ['T1','Asha','PGT'] and ['T2','Ravi','TGT'], Search_Teacher() prints:

['T1', 'Asha', 'PGT'] …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.