Q.(a) Write a point of difference between append
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.