471,050 Members | 1,125 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,050 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 2578
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Hal Vaughan | last post: by
7 posts views Thread by Donna Hawkins | last post: by
1 post views Thread by Peter Kirk | last post: by
8 posts views Thread by Andreas Klemt | last post: by
6 posts views Thread by cmk128 | last post: by
18 posts views Thread by Jordan Glassman | last post: by
18 posts views Thread by Paul Lautman | last post: by

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.