Redirecting STDOUT to a Python Variable

A

Anthony Papillion

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?
 
P

Paul Rubin

Anthony Papillion said:
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.
 
J

James Mills

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
 
S

Steven D'Aprano

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:

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)
 
R

ross.marsden

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)
 
H

Hans Mulder

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
 
P

Prasad, Ramit

Hans said:
exc_traceback))

Thiscould 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.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top