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!
cxt
Start Free Trial