Write a function to write numbers into a binary file and read the same.
Answer:
def
binary_file
():
import
pickle
f=open(
'data.dat'
,
'wb'
)
while
True
:
x=int(input(
"Enter a number: "
))
pickle.dump(x,f)
more=input(
'Want to enter more numbers? Enter Y is yes: '
)
if
more.upper() !=
'Y'
:
break
f.close()
print(
"Reading from the binary file"
)
f=open(
'data.dat'
,
'rb'
)
try
:
while
True
:
output=pickle.load(f)
print(output)
except
EOFError:
pass
f.close()