putting the output of a print statement into a string

Jon Perez jbperez808 at wahoo.com
Sun Mar 27 17:42:53 EST 2005


There are objects whose repr() is not the same
as what gets printed out when you apply the
print statement to them.  Usually these are
complex objects like exceptions.

Example:

>>> import smtplib

>>> server=smtplib.SMTP("smtp.yourisp.com")

>>> try:
          server.sendmail("xxx at in88validdomain.com",
                          "xxx at abc.com","message")

    except smtplib.SMTPRecipientsRefused, senderrs:
          exc_obj=senderrs

>>> repr(exc_obj)
'<smtplib.SMTPRecipientsRefused instance at 0x00EA7C60>'

>>> print exc_obj
{'xxx at abc.com': (550, 'Verification failed for <xxx at in88validdomain.com>\nunrouteable mail domain "in88validdomain.com"\nSender
verify failed')}


Question:

Is there a way to somehow put the output of 'print exc_obj' into
a string?







More information about the Python-list mailing list