Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Copy Stdout to string

Reply
Thread Tools

Copy Stdout to string

 
 
sophie_newbie
Guest
Posts: n/a
 
      04-01-2008
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!
 
Reply With Quote
 
 
 
 
Ryan Ginstrom
Guest
Posts: n/a
 
      04-01-2008
> 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

 
Reply With Quote
 
 
 
 
Gerard Flanagan
Guest
Posts: n/a
 
      04-01-2008
On Apr 1, 4:03 pm, sophie_newbie <paulgeele...@gmail.com> 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()
---------------------------------------------------

 
Reply With Quote
 
sophie_newbie
Guest
Posts: n/a
 
      04-01-2008
On Apr 1, 3:03 pm, sophie_newbie <paulgeele...@gmail.com> 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/pyth...-same-time/52/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
beginner Q: Kernel#puts, STDOUT, $stdout relation Andreas S Ruby 3 12-09-2006 12:39 AM
what is Deep Copy, shallow copy and bitwises copy.? saxenavaibhav17@gmail.com C++ 26 09-01-2006 09:37 PM
Problems redirecting STDOUT (NOT sys.stdout) to a pipe. Elad Python 0 03-19-2006 01:30 PM
copy stdout fails with permission denied when stdout is redirected brian.mabry.edwards@gmail.com Perl Misc 2 12-07-2005 10:49 PM
is dict.copy() a deep copy or a shallow copy Alex Python 2 09-05-2005 07:01 AM



Advertisments