os.popen on windows: loosing stdout of child process
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Sat May 12 00:22:20 EDT 2007
En Sat, 12 May 2007 00:46:16 -0300, Greg Ercolano <erco at 3dsite.com>
escribió:
> 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: [...]
> When I run the same python program on a unix box, the output
> from 'nslookup' appears in the terminal, as I'd expect.
>
> Shouldn't popen() be consistent in its handling of the child's
> stdout and stderr across platforms?
>
> Maybe I'm missing something, being somewhat new to python, but
> an old hand at unix and win32 and functions like popen(). Didn't
> see anything in the docs for popen(), and I googled around quite
> a bit on the web and groups for eg. 'python windows popen stdout lost'
> and found nothing useful.
Using the subprocess module is the recommended approach (as you can see on
the os.popen documentation) and does what you want:
C:\TEMP>type foo2.py
import subprocess
p = subprocess.Popen("nslookup", stdin=subprocess.PIPE)
p.stdin.write("google.com\n")
p.stdin.close()
C:\TEMP>python foo2.py
C:\TEMP>Servidor predeterminado: coyote.softlabbsas.com.ar
Address: 192.168.0.116
> Servidor: coyote.softlabbsas.com.ar
Address: 192.168.0.116
Respuesta no autoritativa:
Nombre: google.com
Addresses: 64.233.187.99, 64.233.167.99, 72.14.207.99
>
C:\TEMP>
For more info about subprocess usage, see
http://docs.python.org/lib/module-subprocess.html
--
Gabriel Genellina
More information about the Python-list
mailing list