English

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 - Computer Science (Python)

Advertisements
Advertisements

Question

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)

Answer in Brief

Solution

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
  Is there an error in this question or solution?
Chapter 2: File Handling in Python - Exercise [Page 38]

APPEARS IN

NCERT Computer Science [English] Class 12
Chapter 2 File Handling in Python
Exercise | Q 10. | Page 38

RELATED QUESTIONS

There are ______ of files that can be handled in python.


Which of the following modes is used for both writing and reading from a binary file?


Which of the following modes is used for both writing and reading from a binary file?


What is not true for binary files?


Which is not an encoding scheme for storing text file?


Every line of text file terminates with a special character called ______.


If you open a binary file you will see ______.


The files that consist of human readable characters ______.


Trying to open a binary file using a text editor will show: 


File that requires specific programs to access its contents.


Differentiate between text file and binary file.


Write the file mode that will be used for opening the following file. Also, write the Python statement to open the following file:

a text file “example.txt” in both read and write mode


Write the file mode that will be used for opening the following file. Also, write the Python statement to open the following file:

a binary file “bfile.dat” in write mode.


Write the file mode that will be used for opening the following file. Also, write the Python statement to open the following file:

a text file “try.txt” in append and read mode.


Write the file mode that will be used for opening the following file. Also, write the Python statement to open the following file:

a binary file “btry.dat” in read only mode.


Assertion (A): CSV (Comma Separated Values) is a file format for data storage that looks like a text file.

Reason (R): The information is organized with one record on each line and each field is separated by a comma.


Write one difference between CSV and text files.


How are text files different from binary files?


Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×