Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Redirecting STDOUT to a Python Variable

4,229 views
Skip to first unread message

Anthony Papillion

unread,
Jun 22, 2010, 2:10:56 AM6/22/10
to
I'm writing an application that uses the Google Storage Python
library. When an error occurs, the error is printed on the terminal.
What I need to do is intercept that text into a variable so I can run
a re.search() against it and find out what's going on.

I thought doing a output_text = method_name(parameters) would stuff
the output in the output_text variable but it doesn't.

How can I accomplish this?

Paul Rubin

unread,
Jun 22, 2010, 2:30:32 AM6/22/10
to
Anthony Papillion <papi...@gmail.com> writes:
> I'm writing an application that uses the Google Storage Python
> library. When an error occurs, the error is printed on the terminal.
> What I need to do is intercept that text into a variable so I can run
> a re.search() against it and find out what's going on.

I'm unfamiliar with that library, but if you mean it raises an
exception, the thing to do is catch the exception and examine the
response code that it should supply. Actually parsing the text error
message is horrendous (think of internationalization, for example).
Though it is sometimes necessary, if the code throwing the exception is
well designed, it shouldn't require parsing the text.

See the python docs about the try/except statement to learn about
exceptions in Python.

James Mills

unread,
Jun 22, 2010, 2:51:10 AM6/22/10
to python list
On Tue, Jun 22, 2010 at 4:10 PM, Anthony Papillion <papi...@gmail.com> wrote:
> I'm writing an application that uses the Google Storage Python
> library.  When an error occurs, the error is printed on the terminal.
> What I need to do is intercept that text into a variable so I can run
> a re.search() against it and find out what's going on.

I'm not familiar with this library (either), however you
would be better off digging through it's documentation
and/or it's sources and find out how to change where it logs
errors to.

--James

--
--
-- "Problems are solved by method"

Steven D'Aprano

unread,
Jun 22, 2010, 3:19:37 AM6/22/10
to
On Mon, 21 Jun 2010 23:10:56 -0700, Anthony Papillion wrote:

> I'm writing an application that uses the Google Storage Python library.
> When an error occurs, the error is printed on the terminal. What I need
> to do is intercept that text into a variable so I can run a re.search()
> against it and find out what's going on.

If the error is *only* printed to stdout (or stderr), then you can do
this to capture it:


>>> import sys
>>> from StringIO import StringIO
>>> capture = StringIO()
>>> save_stdout = sys.stdout
>>> sys.stdout = capture
>>> print "hello world"
>>> sys.stdout = save_stdout
>>> print capture.getvalue()
hello world

>>>

Don't forget to restore stdout, or you'll have no end of grief later.


But if the error is a proper exception, and you're talking about the
traceback, then wrap the call in a try...except block:


try:
method(args)
except Exception, e:
do_something_with(e)

--
Steven

ross.m...@gmail.com

unread,
Sep 22, 2012, 5:57:52 PM9/22/12
to
To capture the traceback, so to put it in a log, I use this

import traceback

def get_traceback(): # obtain and return the traceback
exc_type, exc_value, exc_traceback = sys.exc_info()
return ''.join(traceback.format_exception(exc_type, exc_value, exc_traceback))


Suppose I have a script run by the scheduler, this captures the traceback form any problems and emails them.



if __name__ == '__main__':
try:
Runs_unattended()
except:
send_mail(send_from = yourEmailAddress,
send_to = [ yourEmailAddress ], subject = 'Runs_unattended',
text = '%s' % get_traceback(),
files = [], server=yourLocalSMTP)

Hans Mulder

unread,
Sep 23, 2012, 3:38:13 AM9/23/12
to
On 22/09/12 23:57:52, ross.m...@gmail.com wrote:
> To capture the traceback, so to put it in a log, I use this
>
> import traceback
>
> def get_traceback(): # obtain and return the traceback
> exc_type, exc_value, exc_traceback = sys.exc_info()
> return ''.join(traceback.format_exception(exc_type, exc_value, exc_traceback))

This could be coded more succinctly as

import sys, traceback

def get_traceback(): # obtain and return the traceback
return ''.join(traceback.format_exception(*sys.exc_info()))

> Suppose I have a script run by the scheduler, this captures the traceback form any problems and emails them.
>
> if __name__ == '__main__':
> try:
> Runs_unattended()
> except:
> send_mail(send_from = yourEmailAddress,
> send_to = [ yourEmailAddress ], subject = 'Runs_unattended',
> text = '%s' % get_traceback(),
> files = [], server=yourLocalSMTP)

Errhm, '%s' % get_traceback() is equiavalent to get_traceback()
How about:

if __name__ == '__main__':
try:
Runs_unattended()
except:
send_mail(send_from = yourEmailAddress,
send_to = [ yourEmailAddress ],
subject = 'Runs_unattended',
text = get_traceback(),
files = [],
server=yourLocalSMTP)



Hope this helps,

-- HansM

Prasad, Ramit

unread,
Sep 27, 2012, 1:09:05 PM9/27/12
to pytho...@python.org
Hans Mulder wrote:
> On 22/09/12 23:57:52, ross.m...@gmail.com wrote:
> > To capture the traceback, so to put it in a log, I use this
> >
> > import traceback
> >
> > def get_traceback(): # obtain and return the traceback
> > exc_type, exc_value, exc_traceback = sys.exc_info()
> > return ''.join(traceback.format_exception(exc_type, exc_value,
> exc_traceback))
>
> This could be coded more succinctly as
>
> import sys, traceback
>
> def get_traceback(): # obtain and return the traceback
> return ''.join(traceback.format_exception(*sys.exc_info()))

Why not just use?

return traceback.format_exc()

>
> > Suppose I have a script run by the scheduler, this captures the traceback
> form any problems and emails them.
> >
> > if __name__ == '__main__':
> > try:
> > Runs_unattended()
> > except:
> > send_mail(send_from = yourEmailAddress,
> > send_to = [ yourEmailAddress ], subject = 'Runs_unattended',
> > text = '%s' % get_traceback(),
> > files = [], server=yourLocalSMTP)
>
> Errhm, '%s' % get_traceback() is equiavalent to get_traceback()
> How about:
>
> if __name__ == '__main__':
> try:
> Runs_unattended()
> except:
> send_mail(send_from = yourEmailAddress,
> send_to = [ yourEmailAddress ],
> subject = 'Runs_unattended',
> text = get_traceback(),
> files = [],
> server=yourLocalSMTP)
>

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.
0 new messages