Using popen in windows

Robert Cragie rcc at nospamthanks_jennic.com
Thu Apr 20 10:16:24 EDT 2000


Gordon McMillan <gmcm at hypernet.com> wrote in message
news:1255890154-29764960 at hypernet.com...
| Robert Cragie wrote:
|
| > I tried the following in Windows 98 as an experiment:
| >
| > from win32pipe import popen
| > p = popen('dir','r')
| > str = p.read()
| > print str
| >
| > It just hangs when trying to do the read, and I have to kill the shell
off.
| > I have tried IDLE 0.5 and PythonWin and the 'DOS' command line, both do
the
| > same. I am using Python 1.5.2, and the win32pipe.pyd DLL is dated 3rd
May
| > 1999.
| >
| > I have done the equivalent on Linux and it works fine:
| >
| > from os import popen
| > p = popen('ls','r')
| > str = p.read()
| > print str
| >
| > Anyone got any ideas?
|
| Ah, but it's not equivalent :-). 'dir' is a cmd.exe builtin, not an
| "external program". You can't (directly) popen any of the
| cmd.exe builtins (and the list changes with each OS release).

I tried this and it still hangs; cvs.exe is definitely not a built in:

from win32pipe import popen
p = popen('c:\\cygwin\\contrib\\bin\\cvs -v','r')
str = p.read()
print str

Again, the equivalent works fine on Linux. So how do I run a command in
Windows and capture the output? The following works, but is clumsy, and you
get the annoying DOS box while it runs - is there a better way?:

import os
os.system('dir >temp')
f = open('temp', 'r')
f.read()

TIA

Robert





More information about the Python-list mailing list