Note that there are some explanatory texts on larger screens.

plurals
  1. POCan you make a python subprocess output stdout and stderr as usual, but also capture the output as a string?
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/4335587/wrap-subprocess-stdout-stderr">Wrap subprocess&#39; stdout/stderr</a> </p> </blockquote> <p>In <a href="https://stackoverflow.com/questions/9859446/subprocess-output-to-stdout-and-to-pipe">this question</a>, <a href="https://stackoverflow.com/users/963318/hanan-n">hanan-n</a> 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:</p> <pre><code>output = [] p = subprocess.Popen(["the", "command"], stdout=subprocess.PIPE) for line in iter(p.stdout.readline, ''): print(line) output.append(line) </code></pre> <p>However, this solution doesn't generalise to the case where you want to do this for both stdout and stderr, while satisfying the following:</p> <ul> <li>the output from stdout/stderr should go to the parent process' stdout/stderr respectively</li> <li>the output should be done in real time as much as possible (but I only need access to the strings at the end)</li> <li>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?)</li> </ul> <p>I looked through the <a href="http://docs.python.org/dev/library/subprocess.html" rel="nofollow noreferrer">subprocess documentation</a>, but couldn't find anything that can achieve this. The closest I could find is to add <code>stderr=subprocess.stdout</code> 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 <code>p.stdout</code> and <code>p.stderr</code>.</p> <p>Here is an example of what I would like to do:</p> <pre><code>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] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload