473,403 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Correctly reading stdout/stderr from subprocess

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
0 2411

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Andrei D. | last post by:
Hello Python newsgroup, In the process of developing a big ssh wrapper for sending commands to multiple hosts over the last few months, I (almost accidentally, considering I'm really just an...
0
by: Roman Neuhauser | last post by:
Hello, I have a piece of code that gets run in a script that has its stdout closed: import sys sys.stdout = sys.stderr c = subprocess.Popen (..., stdin = subprocess.PIPE,
3
by: alf | last post by:
Hi, I use subprocess to execute another program but need to suppress its stdout. I can achieve it by using Popen(...,stdout=subprocess.PIPE,...) but wonder where the all stdout actually goes. Is...
3
by: Fuzzyman | last post by:
Hello all, Before I ask the question a couple of notes : * This question is for implementing a script inside the Wing IDE. For some reason using the subprocess module doesn't work so I need a...
2
by: gbastian | last post by:
Dear Users, I am trying to recover through the python function popen3 the stdout,in,err of a launched process. I would like also to recover the stdout which you can get only through the...
2
by: Greg Ercolano | last post by:
When I use os.popen(cmd,'w'), I find that under windows, the stdout of the child process disappears, instead of appearing in the DOS window the script is invoked from. eg: C:\type foo.py import...
2
by: Murali | last post by:
Hi Python programmers, I need to be able to read the stdout and stderr streams of an external program that I launch from my python script. os.system( 'my_prog' + '>& err.log' ) and was planning...
2
by: sophie_newbie | last post by:
Hi, I'm wondering if its possible to copy all of stdout's output to a string, while still being able to print on screen. I know you can capture stdout, but I still need the output to appear on the...
2
by: newlasdjfk | last post by:
I have read tons of posts but still can't seem to figure it out. I want to subprocess.Popen() rsync.exe in windows, and print the stdout in python. This works... import subprocess, time, os,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.