Copy Stdout to string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sophie_newbie

    Copy Stdout to string

    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!
  • Gerard Flanagan

    #2
    Re: Copy Stdout to string

    On Apr 1, 4:03 pm, sophie_newbie <paulgeele...@g mail.comwrote:
    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.Pope n(cmdline,
    stdout=subproce ss.PIPE,
    stderr=subproce ss.STDOUT).stdo ut

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

    print

    print buf.getvalue()
    ---------------------------------------------------

    Comment

    • sophie_newbie

      #3
      Re: Copy Stdout to string

      On Apr 1, 3:03 pm, sophie_newbie <paulgeele...@g mail.comwrote:
      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:


      Comment

      Working...