Skip to content
Question

Q.(a) Write one difference between CSV and text files. Write a program in Python that defines and calls the following user defined functions:

(i) COURIER_ADD(): It takes the values from the user and adds the details to a csv file 'courier.csv'. Each record consists of a list with field elements as cid, s_name, Source, destination to store Courier ID, Sender name, Source and destination address respectively.
(ii) COURIER_SEARCH(): Takes the destination as the input and displays all the courier records going to that destination.
(OR)
(b) Why is important to close a file before exiting? Write a program in Python that defines and calls the following user defined functions:
(i) Add_Book(): Takes the details of the books and adds them to a csv file 'Book.csv'. Each record consists of a list with field elements as book_ID, B_name and pub to store book ID, book name and publisher respectively.
(ii) Search_Book(): Takes publisher name as input and counts and displays number of books published by them.
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): CSV is delimited/tabular while text is free-form; COURIER_ADD() appends a record and COURIER_SEARCH() prints records matching a destination.

Part (b): a file must be closed so buffered data is flushed and resources freed; Add_Book() appends a record and Search_Book() counts a publisher's books.

Part (a)

One difference between CSV and text files. A CSV (Comma-Separated Values) file is structured: each line is one record and its fields are separated by a delimiter (a comma), so it reads like a table. A plain text (.txt) file is unstructured — it just holds characters with no fixed field layout.

We use Python's csv module. COURIER_ADD() opens the file in append mode ('a', so existing records are kept) with newline='' (avoids blank lines on Windows) and writes a four-field list. COURIER_SEARCH() reads every row and prints those whose destination field matches the input.

import csv

def COURIER_ADD():
    with open('courier.csv', 'a', newline='') as f:
        w = csv.writer(f)
        cid = input("Courier ID: ")
        s_name = input("Sender name: ")
        Source = input("Source: ")
        destination = input("Destination: ")
        w.writerow([cid, s_name, Source, destination])
    print("Record added.")

def COURIER_SEARCH():
    dest = input("Destination to search: ")
    found = False
    with open('courier.csv', 'r', newline='') as f:
        for rec in csv.reader(f):
            if len(rec) == 4 and rec[3] == dest:
                print(rec)
                found = True
    if not found:
        print("No couriers for this destination.")

COURIER_ADD()
COURIER_SEARCH()
``` …

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.