Question 10 of 10advanced🔧 ApplyLong Answer5 marks

Write a program to enter the following records in a binary file: Item No: integer Item_Name: string Qty: integer Price: float Number of records to be entered should be accepted from the user. Read the file to display the records in the following format: Item No: Item Name: Quantity: Price per item: Amount: (to be calculated as Price * Qty)

Correct Answer

python import pickle

Accepting number of records from user

n = int(input("Enter number of records to be entered: "))

Writing records to binary file

bfile = open("itemfile.dat", "ab")

for i in range(n): print("\nRECORD No.", i+1) ino = int(input("Item No: ")) iname = input("Item Name: ") qty = int(input("Quantity: ")) price = float(input("Price: ")) itemdata = [ino, iname, qty, price] pickle.dump(itemdata, bfile)

bfile.close()

Reading records from binary file

print("\nDisplaying Records:\n") try: with open("itemfile.dat", "rb") as bfile: while True: itemdata = pickle.load(bfile) amount = itemdata[2] * itemdata[3] print("Item No:", itemdata[0]) print("Item Name:", itemdata[1]) print("Quantity:", itemdata[2]) print("Price per item:", itemdata[3]) print("Amount:", amount) print() except EOFError: pass

The program uses the pickle module to handle binary file operations. First, it accepts the number of records from the user and opens the file in append binary mode ("ab") to write data.

Each record is stored as a list containing Item No, Item Name, Qty, and Price. The pickle.dump() function serializes and writes each record to the file. After writing, the file is closed.

For reading, the file is opened in read binary mode ("rb"). The pickle.load() function deserializes each record inside an infinite while loop. A try..except block handles the EOFError exception to detect the end of file.

The Amount is calculated as Price * Qty and displayed along with other fields in the specified format.

Exercise: EXERCISE | Q: 10 | (Page 20)
For More Understanding

Explanation

This question tests the student's understanding of binary file handling using the pickle module. The textbook demonstrates this concept through Program 2-8, which shows how to write employee records as lists to a binary file and read them back. The key concepts include: using pickle.dump() to serialize and write Python objects, using pickle.load() to deserialize and read objects, and handling EOFError to detect end-of-file. Students must adapt the employee record example to create an item record program with the specified fields and output format.

Solution Steps

  1. Step 1: Import pickle module for binary file operations

  2. Step 2: Accept number of records from user using input()

  3. Step 3: Open file in 'ab' (append binary) mode for writing

  4. Step 4: Use for loop to accept n records and store each as a list

  5. Step 5: Use pickle.dump() to write each record list to file

  6. Step 6: Close the file after writing

  7. Step 7: Open file in 'rb' (read binary) mode for reading

  8. Step 8: Use try..except with while loop and pickle.load() to read records

  9. Step 9: Calculate Amount as Price * Qty and display in specified format

  10. Step 10: Handle EOFError exception to stop reading at end of file