Q. Following is the structure of each record in a data file named "PRODUCT.DAT".
("prod_code": value, "prod_desc": value, "stock": value)
The values for prod_code and prod_desc are strings and the value for stock is an integer.
Write a function in Python to update the file with a new value of stock. The stock and the product_code, whose stock is to be updated, are to be inputted during the execution of the function.
Answer =
import pickle
f = open("pathwala.txt","rb")
datalst = [ ]
while True :
try :
data = pickle.load(f)
datalst += [data]
except EOFError :
break
f.close()
f = open("pathwala.txt","wb")
stock = int(input("Enter Stock which is to update :-"))
for i in datalst :
if stock == i["stock"] :
prod_code = input("Enter prod_code :- ")
prod_desc = input("Enter prod_desc :- ")
i["prod_code"] = prod_code
i [ "prod_desc" ] = prod_desc
pickle.dump(datalst,f)
f.close()
Why were u using text file instead of using binary file
ReplyDeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )