0% found this document useful (0 votes)
343 views2 pages

Binary Example PGM

The document describes a Python program for managing passenger records stored in a binary file named PASSENGERS.DAT. It includes three user-defined functions: Create() for inputting and saving passenger data, SearchDestn(D) for retrieving passengers based on their destination, and UpdateFare() for increasing the fare of all passengers by 5%. The program utilizes the pickle module for serialization and deserialization of passenger records.

Uploaded by

srisuppu1763
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
343 views2 pages

Binary Example PGM

The document describes a Python program for managing passenger records stored in a binary file named PASSENGERS.DAT. It includes three user-defined functions: Create() for inputting and saving passenger data, SearchDestn(D) for retrieving passengers based on their destination, and UpdateFare() for increasing the fare of all passengers by 5%. The program utilizes the pickle module for serialization and deserialization of passenger records.

Uploaded by

srisuppu1763
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

A file, PASSENGERS.

DAT, stores the records of passengers using the following


structure:
[PNR, PName, BRDSTN, DESTN, FARE]
where:
PNR -Passenger Number (string type)
PName -Passenger Name (string type)
BRDSTN - Boarding Station Name (string type)
DESTN - Destination Station Name (string type)
FARE - Fare amount for the journey (float type)
Write user defined functions in Python for the following tasks:
(1) Create() to input data for passengers and write it in the binary file
PASSENGERS.DAT.
(ii) SearchDestn (D) - to read contents from the file PASSENGERS.DAT and display
the details of those Passengers whose DESTN matches with the value of D.
(iii) UpdateFare () - to increase the fare of all passengers by 5% and rewrite the
updated records into the file PASSENGERS.DAT.

1.

import pickle
def Create():
f=open("PASSENGERS.DAT", "wb")
while True:
PNR=input("PNR No:")
PName=input("Name: ")
BRDSTN=input("Boarding at: ")
DESTN=input("Destination: ")
FARE=float(input("Fare: "))
rec=[PNR,PName,BRDSTN,DESTN,FARE]
pickle.dump(rec,f)
r = input("To continue press 'y' or press 'n':")
if r == 'n':
break
f.close()
Create()
2.
import pickle
def SearchDestn(D):
f=open("PASSENGERS.DAT", "rb")
try:
while True:
rec=pickle.load(f)
if rec[3]==D :
print(rec)
except EOFError:
print("EOF reached")
f.close()
SearchDestn('chennai')

3.
import pickle
def UpdateFare():
newrec = []
try:
f = open("PASSENGERS.DAT", "rb")
while True:
rec = pickle.load(f)
rec[4] += (rec[4] * 0.05)
newrec.append(rec)
except EOFError:
f.close()

with open("PASSENGERS.DAT", "wb") as f:


for rec in newrec:
pickle.dump(rec, f)

UpdateFare()

You might also like