By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,148 Members | 739 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,148 IT Pros & Developers. It's quick & easy.

Correctly reading stdout/stderr from subprocess

P: n/a
Evening,

I'm having trouble with running a process through Python 2.4's
subprocess module. Example code:

================================================== ======
def run(command):
run = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

# Wait for the process to return
returncode = run.wait()
stdout = run.stdout.readlines()
stderr = run.stderr.readlines()

# Strip newlines at the end of each line
stdout = [line.rstrip('\n') for line in stdout]
stderr = [line.rstrip('\n') for line in stderr]

return returncode,stdout,stderr
================================================== ======

Unfortunately this def fails when it runs programs that do a lot of
output. I believe this problem has also been described in Bug #1162428.
It says: "You must read away the data before wait() on the process."
Easier said than done. If I put the readlines() call before the
run.wait() then the process hangs, too, waiting for further lines.

Then I read about the communicate() call which handles both
stdout/stderr reading and also waiting for the process to end.
Unfortunately it returned the output char-wise instead of line-wise. I
could certainly re-join the lines. But somehow I feel I'm missing
something simple.

Any ideas?

Kindly
Christoph

Jun 13 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.