i stored data in a binary file.
enter code here
code 1--
import pickle as p
d = []
for i in range(3):
d1 = []
st = input("enter student name")
rl = int(input("student roll no"))
d1.append(st)
d1.append(rl)
d.extend(d1)
f = open("alex.dat", "wb")
p.dump(d,f)
f.close()
and then i printed
code 2--
import pickle as p
d = []
f = open("students.dat", "rb")
while f:
try:
d = p.load(f)
print(d)
except EOFError:
f.close()
output -- ['admin', 22, 'momo', 21, 'sudhanshu', 323] Traceback (most recent call last): File "C:\Users\admin\AppData\Roaming\JetBrains\PyCharmCE2021.3\scratches\scratch_2.py", line 6, in d = p.load(f) ValueError: peek of closed file
why valueError?
As @Maurice Mayer stated the while Condition is breaking your Code
You are writing in Code 1 everything in one file so you need just to load the file once. Checking the file-object which is already closed is breaking your Code 2
import pickle as p
d = None # Just to be sure
f = open("students.dat", "rb")
try:
d = p.load(f)
print(d)
except EOFError:
f.close()
This should work
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.