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

  • Home
  • SEARCH
  • 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 7037311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:31:45+00:00 2026-05-28T01:31:45+00:00

This is a follow up question from here specifically concerning its answer . From

  • 0

This is a follow up question from here specifically concerning its answer.


From a python module I am calling a Hello World executable that simply prints Hello World to the stdout. I am interested in redirecting that output to a python StringIO and ran into this answer which almost brings me all the way to the solution.

The critical part of this answer is this code segment:

1. def redirect_stdout():
2.     print "Redirecting stdout"
3.     sys.stdout.flush() # <--- important when redirecting to files
4.     newstdout = os.dup(1)
5.     devnull = os.open('/dev/null', os.O_WRONLY)
6.     os.dup2(devnull, 1)
7.     os.close(devnull)
8.     sys.stdout = os.fdopen(newstdout, 'w')

Also I would like to restore the stdout as it was before the redirection.

Questions

  1. What exactly is going on in the function above?
    • What is dup and dup2 doing?
    • What is /dev/null?
    • What is line 8 doing? (sys.stdout = os.fdopen(newstdout, 'w'))
  2. How can I store the stdout in a StringIO object?
  3. How can I restore the stdout after the call to my Hello World program?

I am pretty sure that once I have the answer for my question 1 that the answers of questions 2 and 3 will be easy. I decided to post them anyway to maybe push the answer of question 1 into the direction where I want to go.

  • 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-28T01:31:46+00:00Added an answer on May 28, 2026 at 1:31 am

    I’ve written below a few additional comments that should make clearer what it’s going on inside the redirect_stdout function:

    def redirect_stdout():
        print "Redirecting stdout"
        sys.stdout.flush() # <--- important when redirecting to files
    
        # Duplicate stdout (file descriptor 1)
        # to a different file descriptor number
        newstdout = os.dup(1)
    
        # /dev/null is used just to discard what is being printed
        devnull = os.open('/dev/null', os.O_WRONLY)
    
        # Duplicate the file descriptor for /dev/null
        # and overwrite the value for stdout (file descriptor 1)
        os.dup2(devnull, 1)
    
        # Close devnull after duplication (no longer needed)
        os.close(devnull)
    
        # Use the original stdout to still be able
        # to print to stdout within python
        sys.stdout = os.fdopen(newstdout, 'w')
    

    One important thing to note is that a process gets three different file descriptors from the OS when launched:

    • stdin: 0
    • stdout: 1
    • stderr: 2

    As explained in the comments, the code above takes advantage of the file descriptor for stdout and the file descriptor duplication functions to make trick the C code into using a different stdout while still keeping a reference to the original stdout in the python code to be able to print.

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

Sidebar

Related Questions

This is a follow-on question from the one I asked here . Can constraints
All, this is a follow up from a previous question here: C# formatting external
Hello and thank you in advance. This is a follow up question from the
So this question is a sort of follow on from here ( how to
This is a follow up question from here . Where I want do go
This is a follow up question from my previous one found here I need
This is a follow-up question from here . The code is working great, it
This is a follow on question from my previously answered question here: Reading characters
This is a follow-up to my question from yesterday . I have Scott Meyers'
This is a follow on question to How do I delete 1 file from

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.