Advertisement

01.30.2007 at 03:10AM PST, ID: 22141856
[x]
Attachment Details

Executing a subprocess and reading its output on windows

Asked by cxt in Python Scripting Language

Tags: , ,

This should be fairly easy but I can't get things to work correctly. Basically I am just trying to launch a command line argument from python and read the standard output. I first tried:

res = os.popen(cmdline, 'r')
for line in res.readlines():
   ...

The problem is that when the res.readlines() is executed, the process may still be running, and thus I am reading a partial output. I have no way to know whether I should wait or if I should proceed.

I then stumbled upon subprocess.Popen which has a wait() member. However, for some reason this does not capture the stdout very well. For example, a subprocess.Popen('cmd /C dir') will print the result on screen rather than capturing it in its result.

Finally, there is a win32pipe module with a popen and a WaitNamedPipe, but I couldn't make this to work and the documentation seems to be sparse.

Surely there must be a simple solution to this simple and common task..

Any help greatly appreciated!

cxtStart Free Trial
 
Loading Advertisement...
 
[+][-]01.30.2007 at 05:37AM PST, ID: 18427422

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.30.2007 at 06:27AM PST, ID: 18427775

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.30.2007 at 08:13AM PST, ID: 18428691

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.30.2007 at 09:12AM PST, ID: 18429178

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.30.2007 at 02:11PM PST, ID: 18431753

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.30.2007 at 04:15PM PST, ID: 18432484

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Python Scripting Language
Tags: python, subprocess, output
Sign Up Now!
Solution Provided By: RichieHindle
Participating Experts: 2
Solution Grade: A
 
 
[+][-]01.30.2007 at 11:56PM PST, ID: 18434153

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.31.2007 at 12:08AM PST, ID: 18434199

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32