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

Discussion in 'Python' started by iMath, Dec 26, 2012.

  1. iMath

    iMath Guest

    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()
     
    iMath, Dec 26, 2012
    #1
    1. Advertising

  2. iMath

    Andrew Berg Guest

    On 2012.12.25 23:26, iMath wrote:
    > why print(e) cannot print out any information ?

    If you want to manipulate tracebacks, use sys.exc_info() and the
    traceback module from the standard library. The logging module also
    comes with an exception() function and an exception() method for Logger
    objects that will log tracebacks.

    http://docs.python.org/3/library/sys.html#sys.exc_info
    http://docs.python.org/3/library/traceback.html
    http://docs.python.org/3/library/logging.html

    --
    CPython 3.3.0 | Windows NT 6.2.9200
     
    Andrew Berg, Dec 26, 2012
    #2
    1. Advertising

  3. iMath

    Peter Otten Guest

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

    iMath wrote:

    > 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>.
     
    Peter Otten, Dec 26, 2012
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,965
    Smokey Grindel
    Dec 2, 2006
  2. iMath
    Replies:
    7
    Views:
    224
    Steven D'Aprano
    Jan 7, 2013
  3. iMath
    Replies:
    3
    Views:
    212
    iMath
    Oct 18, 2012
  4. iMath
    Replies:
    8
    Views:
    203
    Steven D'Aprano
    Oct 9, 2012
  5. iMath
    Replies:
    11
    Views:
    219
Loading...

Share This Page