By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,270 Members | 1,703 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,270 IT Pros & Developers. It's quick & easy.

Copy Stdout to string

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.