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 input during the execution of the function.
Answers
Answer:
fp = open("file.txt", 'r')
fp.read()
(b) fp=open("file.txt", 'r')
4. What is the output of the following code fragment? Explain.
fout = file("output.txt", 'w')
fout.write("Hello, world!\n")
fout.write("How are you?")
fout.close()
file("output.txt").read()
5. Write the output of the following code with justification if the contents of the file ABC.txt are:
Welcome to Python Programming!
f1 = file("ABC.txt", "r")
size = len(f1.read())
print(size)
data = f1.read(5)
print(data)
6. Anant has been asked to display all the students who have scored less than 40 for Remedial Classes. Write
a user-defined function to display all those students who have scored less than 40 from the binary file
"Student.dat".
7. Give the output of the following snippet:
import pickle
list1, list2 = [2, 3, 4, 5, 6, 7, 8, 9, 10], []
for i in list1:
if (i%2==0 and i%4==0):
list2.append(i)
f = open("bin.dat","wb")
pickle.dump(list2, f)
f.close()
f = open("bin.dat", "rb")
data = pickle.load(f)
f.close()
for i in data:
print(i)
8. 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.
Computer Science with Python–XII 4.24 9. Given a binary file “STUDENT.DAT”, containing records of the following type:
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.