EOFError why print(e) cannot print out any information ?

I

iMath

why print(e) cannot print out any information ?

class user:
def __init__(self, x,y,z):
self.id = x
self.name = y
self.emailadd=z
def dispuser(self):
print('User ID: ', self.id)
print('User Name : ', self.name)
print('Email Address: ', self.emailadd)

f = open('UsersInfo.bin', 'wb')
n=int(input('How many users?'))
print('Enter ', n, 'numbers')
for i in range(0,n):
u=input('User ID: ')
n=input('User Name: ')
e=input('Email Address: ')
usrobj=user(u,n,e)
pickle.dump(usrobj,f)
f.close()
print('\nInformation of the users is:')
f = open('UsersInfo.bin','rb')
while True:
try:
usrobj = pickle.load(f)
except EOFError as e:
print(e)
break
else:

usrobj.dispuser()
f.close()
 
P

Peter Otten

iMath said:
f = open('UsersInfo.bin','rb')
while True:
try:
usrobj = pickle.load(f)
except EOFError as e:
print(e)
break
else:
usrobj.dispuser()
f.close()
why print(e) cannot print out any information ?

Because the relevant code in pickle doesn't supply an error message; it's
just

raise EOFError

While you can add the information yourself by subclassing the Unpickler...

class MyUnpickler(pickle.Unpickler):
def __init__(self, file, **kw):
super().__init__(file, **kw)
self.file = file
def load(self):
try:
return super().load()
except EOFError:
raise EOFError("EOFError: Reached end of {}".format(self.file))

with open('UsersInfo.bin','rb') as f:
load = MyUnpickler(f).load
while True:
try:
usrobj = load()
except EOFError as e:
print(e)
break
else:
usrobj.display()

.... it might be a good idea to make a feature request on
<http://bugs.python.org>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top