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 8483781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:12:02+00:00 2026-06-10T20:12:02+00:00

Possible Duplicate: Wrap subprocess' stdout/stderr In this question , hanan-n asked whether it was

  • 0

Possible Duplicate:
Wrap subprocess' stdout/stderr

In this question, hanan-n asked whether it was possible to have a python subprocess that outputs to stdout while also keeping the output in a string for later processing. The solution in this case was to loop over every output line and print them manually:

output = []
p = subprocess.Popen(["the", "command"], stdout=subprocess.PIPE)
for line in iter(p.stdout.readline, ''):
    print(line)
    output.append(line)

However, this solution doesn’t generalise to the case where you want to do this for both stdout and stderr, while satisfying the following:

  • the output from stdout/stderr should go to the parent process’ stdout/stderr respectively
  • the output should be done in real time as much as possible (but I only need access to the strings at the end)
  • the order between stdout and stderr lines should not be changed (I’m not quite sure how that would even work if the subprocess flushes its stdout and stderr caches at different intervals; let’s assume for now that we get everything in nice chunks that contain full lines?)

I looked through the subprocess documentation, but couldn’t find anything that can achieve this. The closest I could find is to add stderr=subprocess.stdout and use the same solution as above, but then we lose the distinction between regular output and errors. Any ideas? I’m guessing the solution – if there is one – will involve having asynchronous reads to p.stdout and p.stderr.

Here is an example of what I would like to do:

p = subprocess.Popen(["the", "command"])
p.wait()  # while p runs, the command's stdout and stderr should behave as usual
p_stdout = p.stdout.read()  # unfortunately, this will return '' unless you use subprocess.PIPE
p_stderr = p.stderr.read()  # ditto
[do something with p_stdout and p_stderr]
  • 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-06-10T20:12:03+00:00Added an answer on June 10, 2026 at 8:12 pm

    This example seems to work for me:

    # -*- Mode: Python -*-
    # vi:si:et:sw=4:sts=4:ts=4
    
    import subprocess
    import sys
    import select
    
    p = subprocess.Popen(["find", "/proc"],
        stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    
    stdout = []
    stderr = []
    
    while True:
        reads = [p.stdout.fileno(), p.stderr.fileno()]
        ret = select.select(reads, [], [])
    
        for fd in ret[0]:
            if fd == p.stdout.fileno():
                read = p.stdout.readline()
                sys.stdout.write('stdout: ' + read)
                stdout.append(read)
            if fd == p.stderr.fileno():
                read = p.stderr.readline()
                sys.stderr.write('stderr: ' + read)
                stderr.append(read)
    
        if p.poll() != None:
            break
    
    print 'program ended'
    
    print 'stdout:', "".join(stdout)
    print 'stderr:', "".join(stderr)
    

    In general, any situation where you want to do stuff with multiple file descriptors at the same time and you don’t know which one will have stuff for you to read, you should use select or something equivalent (like a Twisted reactor).

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

Sidebar

Related Questions

I have a PowerShell script that outputs to stdout and stderr. I'd like to
Possible Duplicate: Gesture detection and ScrollView issue EDIT: question with full code asked here
Possible Duplicate: Is it a good idea to wrap an #include in a namespace
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: check whether internet connection is available with C# I just want to
Possible Duplicate: Python : how to append new elements in a list of list?
Possible Duplicate: Get the cartesian product of a series of lists in Python I'm
Possible Duplicate: regex for URL including query string I have a text or message.
Possible Duplicate: Wrap every 3 divs in a div First thing, i know i

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.