Advertisements
Advertisements
प्रश्न
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)
संक्षेप में उत्तर
Advertisements
उत्तर
Writing records in a Binary File -
import pickle
bin_file = open("Items.dat", "wb")
record = {}
wish = "Y"
count = 1
while wish.upper() == "Y":
print("Enter Following details of Item #",count)
item_no = int(input("Enter Item Number : "))
item_name = input("Enter Item Name : ")
quantity = int(input("Enter Quantity : "))
price = float(input("Enter Price per Item : "))
record['itemno"] = item_no
record['itemname"] = item_name
record['quantity"] = quantity
record['price"] = price
pickle.dump(record, bin_file)
wish = input("Want to Add More Records (Y/N) : ")
count = count + 1
bin_file.close()
Reading records from a Binary File -
import pickle
bin_file = open("Items.dat", "rb")
record = {}
count = 1
try:
while True:
print("Item #",count,"Details")
record = pickle.load(bin_file)
print("Item Number : ",record['itemno'])
print("Item Name : ",record['itemname'])
print("Quantity : ",record['quantity'])
print("Price : ", record['price'])
print("Amount : ",record['quantity'] * record['price'])
count = count + 1
except EOFError:
bin_file.close()shaalaa.com
Types of Files in Python
क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
