Q. Write a Python program to create a binary file Itemnew.dat storing all the item details of binary file Items.dat except for the item whose itemno is obtained from the user, e.g., if user gives Itemno as 1005, then Itemnew.dat should store all 'items' details of Items.dat except item 1005.


Answer :-

import pickle
file = open("items.dat","rb")
data = []
try :
      while True :
            data.append( pickle.load( file ) )
except :
      file.close()

for i in range( len( data )):
      for j in range( len( data ) - 1 ):
            if data [j][0] > data [j + 1][0] :
                  data [j][0] , data [j + 1][0] = data [j + 1][0] , data [j][0]

itemno = int(input("Enter item Number ( which will not write in Itemnew.dat ):- "))
print()
file = open("Itemnew.dat","wb")
for i in data :
      if i [0] != itemno:
            pickle.dump( i , file )
file.close()

print("Content in Itemnew.dat :- ")
print()
file = open("Itemnew.dat","rb")
try :
      while True :
            print( pickle.load(file) )
except :
      file.close()

Output :-


Enter item Number ( which will not write in Itemnew.dat ):- 6541

Content in Itemnew.dat :- 

[4896, 'Computer Portal', 796.58, 'Youtube']
[6544, 'Path Walla', 565.985, 'Website']
[75698, 'Portal Express', 564.0, 'youtube']

>>>

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post