467,877 Members | 1,093 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,877 developers. It's quick & easy.

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!
Apr 1 '08 #1
  • viewed: 7461
Share:
2 Replies
On Apr 1, 4:03 pm, sophie_newbie <paulgeele...@gmail.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.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()
---------------------------------------------------

Apr 1 '08 #2
On Apr 1, 3:03 pm, sophie_newbie <paulgeele...@gmail.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:

http://www.answermysearches.com/pyth...-same-time/52/
Apr 1 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by ma740988 | last post: by
11 posts views Thread by csudha | last post: by
7 posts views Thread by MisterPete | last post: by
4 posts views Thread by hg | last post: by
11 posts views Thread by Adrian | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.