468,733 Members | 2,180 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,733 developers. It's quick & easy.

Correctly reading stdout/stderr from subprocess


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?


Jun 13 '06 #1
0 2131

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Roman Neuhauser | last post: by
3 posts views Thread by alf | last post: by
2 posts views Thread by gbastian | last post: by
2 posts views Thread by Greg Ercolano | last post: by
2 posts views Thread by sophie_newbie | last post: by
reply views Thread by zhoujie | last post: by
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.