Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6931321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:33:42+00:00 2026-05-27T11:33:42+00:00

I have a bit of an issue here. I have a Python script which

  • 0

I have a bit of an issue here. I have a Python script which calls binaries compiled from C++. The Python script has its own set of outputs (to standard out and error), which are easily disable-able. The C++ binaries have their own set of outputs (to standard out and error, among others) as well; the source can be altered, but I am not the original author. This is an issue because I do not want the C++ output in my final program, and I also don’t want future users to need to edit the C++ source.

What I’d like to be able to do is have some Python method which will catch the C++ code’s output that is sent to standard out or error. Is this possible? If so, could someone point me in the right direction?

Thank you!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:33:43+00:00Added an answer on May 27, 2026 at 11:33 am

    One way to do this is:

    • Duplicate in python the file descriptors for stdout and stderr using os.dup.
    • Redirect the original stdout and stderr using reopen (from C’s stdio) to write to a file of your choice.

    Note: reopen isn’t available directly from python, but you should be able to call it as in the example below or using any other wrapper available.

    After this is done:

    • Every write to cout and cerr in C++ will write to the output files.
    • Every print statement in python will write to the output files.

    However, since the original descriptors are duplicated, you can still (see example below):

    • Print to the original stdout/stderr using sdout.write and stdout.err
    • Use logging methods after configuring properly the stream parameter

    The following code uses instant library to test real C++ code that is wrapped into python using SWIG and that should be similar to the library that you have:

    import sys, os
    import logging
    from instant import inline
    
    print 'This is printed from python to stdout'
    stdout = os.fdopen(os.dup(sys.stdout.fileno()), 'w')
    stderr = os.fdopen(os.dup(sys.stderr.fileno()), 'w')
    
    logging.basicConfig(stream=stderr, level=logging.DEBUG)
    
    redirect = inline("""                                                                                                                    
    void redirect(void) {                                                                                                                    
        freopen("my_stdout.txt", "w", stdout);                                                                                               
        freopen("my_stderr.txt", "w", stderr);                                                                                               
    }                                                                                                                                        
    """)
    redirect()
    
    cout = inline("""                                                                                                                        
    void cout(void) {                                                                                                                        
        std::cout << "This is written from C++ to my_stdout.txt" << std::endl;                                                               
        std::cerr << "This is written from C++ to my_stderr.txt" << std::endl;                                                               
    }                                                                                                                                        
    """)
    cout()
    
    print 'This is written from python to my_stdout.txt'
    
    stdout.write('This is printed from python to stdout\n')
    stderr.write('This is printed from python to stderr\n')
    logging.info('This is printed to stderr from python using logging')
    

    The output for this example is:

    $ python test.py
    This is printed from python to stdout
    This is printed from python to stdout
    This is printed from python to stderr
    INFO:root:This is printed to stderr from python using logging
    $ cat my_stdout.txt 
    This is written from C++ to my_stdout.txt
    This is written from python to my_stdout.txt
    $ cat my_stderr.txt 
    This is written from C++ to my_stderr.txt
    

    Note: First time the code is executed, you might get gcc compilation messages (I’ve removed them to make the example clearer).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a bit of an issue here. We have upgraded from Delphi 2006
hello fellow java developers. I'm having a bit of an issue here. I have
I have a bit of a ridiculous issue here and I must be missing
I have a bit of a strange issue here. I created a custom control
I'm having a strange issue here, i have this bit of code: <?php if
I have an issue here a bit complex than I'm trying to resolve since
I have a bit of an issue here. As Im new to jQuery this
I have a library with C++ python extensions (C++ calls python which in turn
Ok, I have a bit of an issue here. I realize that I don't
I am having a bit of an issue here... I have a Div and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.