Redirecting STDOUT to a Python Variable

Discussion in 'Python' started by Anthony Papillion, Jun 22, 2010.

  1. 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?
    Anthony Papillion, Jun 22, 2010
    #1
    1. Advertising

  2. Anthony Papillion

    Paul Rubin Guest

    Anthony Papillion <> 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.
    Paul Rubin, Jun 22, 2010
    #2
    1. Advertising

  3. Anthony Papillion

    James Mills Guest

    On Tue, Jun 22, 2010 at 4:10 PM, 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.


    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"
    James Mills, Jun 22, 2010
    #3
  4. 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
    Steven D'Aprano, Jun 22, 2010
    #4
  5. Anthony Papillion

    Guest

    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)
    , Sep 22, 2012
    #5
  6. Anthony Papillion

    Hans Mulder Guest

    On 22/09/12 23:57:52, 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
    Hans Mulder, Sep 23, 2012
    #6
  7. Hans Mulder wrote:

    > On 22/09/12 23:57:52, 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))
    >
    > 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.
    Prasad, Ramit, Sep 27, 2012
    #7
    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. Jon Landenburer

    redirecting stderr and stdout

    Jon Landenburer, May 12, 2004, in forum: Perl
    Replies:
    1
    Views:
    11,196
    Joe Smith
    May 13, 2004
  2. Sudsy
    Replies:
    0
    Views:
    4,369
    Sudsy
    Jul 18, 2003
  3. Douwe
    Replies:
    1
    Views:
    5,617
    Mike Schilling
    Jan 12, 2004
  4. Jan Knop
    Replies:
    4
    Views:
    1,009
    Jan knob
    Nov 24, 2003
  5. Elad
    Replies:
    0
    Views:
    397
Loading...

Share This Page