473,395 Members | 2,006 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.

Redirect output

I have a program which is written in C and interfaced with python via
Swig. However, the function I call prints stuff out to the console. I
would like to capture what it is printing out. I tried:

Expand|Select|Wrap|Line Numbers
  1. import MyCProg, sys
  2.  
  3. f = open("tmp.txt", "wb")
  4. sys.stdout = f
  5. MyCProg.getData()
  6. f.close()
  7. sys.stdout = sys.__stdout__
  8.  
  9. print open("tmp.txt", "rb").read()
  10.  
However, it just prints the data to the screen. I tried redirecting
stderr as well.

other than modifying the C code and re-generating the interface via
Swig, any ideas? just asking before i have to do that.

Mar 28 '06 #1
1 2682
In article <11**********************@u72g2000cwu.googlegroups .com>,
"abcd" <co*******@gmail.com> wrote:
I have a program which is written in C and interfaced with python via
Swig. However, the function I call prints stuff out to the console. I
would like to capture what it is printing out. I tried:

Expand|Select|Wrap|Line Numbers
  1.  import MyCProg, sys
  2.  f = open("tmp.txt", "wb")
  3.  sys.stdout = f
  4.  MyCProg.getData()
  5.  f.close()
  6.  sys.stdout = sys.__stdout__
  7.  print open("tmp.txt", "rb").read()
  8.  

However, it just prints the data to the screen. I tried redirecting
stderr as well.

other than modifying the C code and re-generating the interface via
Swig, any ideas? just asking before i have to do that.


As you probably surmised, C I/O doesn't notice when you've
swapped the stdout object in the Python sys module.

If that's what you're trying to do, redirect the output of
something like printf() or puts(), then you might look at
the os.dup2() function as a way to redirect unit 1. Get
the new output unit from os.open() with os.O_CREAT plus whatever
other flags, or open the output file some other way that
creates a file object and use its fileno() function. Flush
stdout before each dup2().

To revert back to the original stdout, you will want a
copy of that stream, which you can get with the os.dup()
function, prior to redirection. All the left over file
descriptors can be closed afterwards.

I assume you're on a UNIX platform or something to that effect.

Donn Cave, do**@u.washington.edu
Mar 28 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Hal Vaughan | last post by:
I have a Java program, composed of a number of classes, that I've been running as a command line program. It logs everything it does and, as it logs each event, it also prints that line of info to...
6
by: Tim Stokes | last post by:
Hi all, I want to do a similar action to response.redirect ("file.htm"). But the file.htm contains some sensitive data. I have placed file.htm in a folder in the InetPub...
7
by: Donna Hawkins | last post by:
I want to use javascript to redirect to a URL which has been passed as a variable (in php). I have searched but cannot find any solution. I think this code is a basic redirect: <script...
2
by: Jason | last post by:
Hello, I have a class, transCore, that does certain work, and by default, prints its progress to the stand output. Later, I will to write a GUI class that encapsulate the transCore class. I...
1
by: Byron | last post by:
I want to take the output of a DOS console application that just streams data to the screen and redirect that output in real time into a C# application that will decode and reformat it for display....
1
by: Peter Kirk | last post by:
Hi there I have a program written by another company (it's a "web control" which returns a web-page: can I compare this to a servlet in the Java world?), which they think is causing problems on...
8
by: Andreas Klemt | last post by:
Hello, I get this error Message "cannot redirect after http headers have been sent" when I do this response.redirect ("home.aspx") How can I find out with vb.net if already a http header has...
6
by: cmk128 | last post by:
Hi here is my c file, compile in gcc 3.X in linux: #include <stdio.h> int main() { printf("Hello\n"); if (fork() == 0) printf("world! \n"); }
18
by: Jordan Glassman | last post by:
Trying to do something fairly routine... drop output into a file to graph, but the following command at the bash command line: ising output produces a blinking cursor, an empty file named...
18
by: Paul Lautman | last post by:
JRough wrote: What do you mean by "redirect the output to Excel"??? Excel isn't a location, it's a spreadsheet program that some (but not all users) will have on their machine. BTW, Location:...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.