Copy Stdout to string

Discussion in 'Python' started by sophie_newbie, Apr 1, 2008.

  1. Hi, I'm wondering if its possible to copy all of stdout's output to a
    string, while still being able to print on screen. I know you can
    capture stdout, but I still need the output to appear on the screen
    also...

    Thanks!
    sophie_newbie, Apr 1, 2008
    #1
    1. Advertising

  2. > On Behalf Of sophie_newbie
    > Hi, I'm wondering if its possible to copy all of stdout's
    > output to a string, while still being able to print on
    > screen. I know you can capture stdout, but I still need the
    > output to appear on the screen also...


    If I understand you correctly, the following class should work.

    from StringIO import StringIO

    class OutBuffer(object):
    """Wraps a stream, and keeps output as a buffer

    Usage:
    >>> import sys
    >>> sys.stdout = OutBuffer(sys.stdout)
    >>> print "spam"

    spam
    >>> print "egg"

    egg
    >>> sys.stdout.getvalue().splitlines()

    ['spam', 'egg']
    """

    def __init__(self, outstream):
    self.out = outstream
    self.buffer = StringIO()

    def write(self, obj):
    """Writes obj to the output stream, storing it to a buffer as
    well"""
    self.out.write(obj)
    self.buffer.write(obj)

    def getvalue(self):
    """Retrieves the buffer value"""
    return self.buffer.getvalue()

    def __getattr__(self, attr):
    """Delegate everything but write and getvalue to the stream"""
    return getattr(self.out, attr)

    Regards,
    Ryan Ginstrom
    Ryan Ginstrom, Apr 1, 2008
    #2
    1. Advertising

  3. On Apr 1, 4:03 pm, sophie_newbie <> wrote:
    > Hi, I'm wondering if its possible to copy all of stdout's output to a
    > string, while still being able to print on screen. I know you can
    > capture stdout, but I still need the output to appear on the screen
    > also...
    >
    > Thanks!


    I don't know if it's what you want, but if you're talking about the
    output of a single command, then the following (or a variation) should
    do. (using 'svn info' as the command).

    ---------------------------------------------------
    import subprocess
    from cStringIO import StringIO
    import sys

    buf = StringIO()

    def popen(cmdline):
    return subprocess.Popen(cmdline,
    stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT).stdout

    for line in popen('svn info'):
    print >> sys.stdout, 'out: ' + line,
    print >> buf, 'buf: ' + line,

    print

    print buf.getvalue()
    ---------------------------------------------------
    Gerard Flanagan, Apr 1, 2008
    #3
  4. On Apr 1, 3:03 pm, sophie_newbie <> wrote:
    > Hi, I'm wondering if its possible to copy all of stdout's output to a
    > string, while still being able to print on screen. I know you can
    > capture stdout, but I still need the output to appear on the screen
    > also...
    >
    > Thanks!


    I found this, it pretty much does the job, easily modified to write to
    a variable instead of a file:

    http://www.answermysearches.com/python-how-to-print-to-screen-and-a-file-at-the-same-time/52/
    sophie_newbie, Apr 1, 2008
    #4
    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. Alex
    Replies:
    2
    Views:
    1,202
  2. Elad
    Replies:
    0
    Views:
    403
  3. Replies:
    26
    Views:
    2,091
    Roland Pibinger
    Sep 1, 2006
  4. Andreas S
    Replies:
    3
    Views:
    245
    Eric Hodel
    Dec 9, 2006
  5. Replies:
    2
    Views:
    319
    A. Sinan Unur
    Dec 7, 2005
Loading...

Share This Page