473,395 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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
2 7929
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: ma740988 | last post by:
There's no way to use the STL algorithm copy to print an outfile (essentially an ofstream)? So now: int main() { std::ifstream InFile( "exercise15.txt"); std::ofstream ToFile( "NewFile.txt"...
1
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
4
by: Oz | last post by:
This is long. Bear with me, as I will really go through all the convoluted stuff that shows there is a problem with streams (at least when used to redirect stdout). The basic idea is that my...
1
by: Patrick Dunnigan | last post by:
Hi, I am attempting a bulk copy from a c program into SQL Server 2000 using DBLib in freeTDS 0.63 RC11 (gcc 3.4.3, RH 9). I am getting an error message that I cannot find any documentation on. ...
11
by: csudha | last post by:
Hi all, Can you please explain me the usage of copy constructor and how do we use it. Give me some example. Regards, Sudha.
13
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a...
7
by: MisterPete | last post by:
How can I inherit from file but stil create an instance that writes to stdout? ----------- I'm writing a file-like object that has verbosity options (among some other things). I know I could just...
4
by: hg | last post by:
Hi, I have the following ********************* C extention - redir.c #include "Python.h" PyObject * test_redir_test(PyObject *self) {
11
by: Adrian | last post by:
Is it possible to save a copy of cout the same way you can save a copy of stdout in C (I know C version is portable, but unix portable is good enough for me). I have to use a library which...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.