os.popen on windows: loosing stdout of child process

Discussion in 'Python' started by Greg Ercolano, May 12, 2007.

  1. 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 os
    import sys
    file = os.popen("nslookup", 'w')
    file.write("google.com\n")
    file.close()

    C:\> python foo.py
    <-- nothing is printed
    C:\>

    This just seems wrong. The following DOS equivalent works fine:

    C:\> echo google.com | nslookup
    Default Server: dns.erco.x
    Address: 192.168.1.14
    [..expected output..]

    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.

    FWIW, I'm using the windows version of python 2.5 from activestate.
     
    Greg Ercolano, May 12, 2007
    #1
    1. Advertising

  2. En Sat, 12 May 2007 00:46:16 -0300, Greg Ercolano <>
    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
     
    Gabriel Genellina, May 12, 2007
    #2
    1. Advertising

  3. Greg Ercolano

    Guest

    On May 11, 8:46 pm, Greg Ercolano <> wrote:
    > 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 os
    > import sys
    > file = os.popen("nslookup", 'w')
    > file.write("google.com\n")
    > file.close()
    >
    > C:\> python foo.py
    > <-- nothing is printed
    > C:\>
    >
    > This just seems wrong. The following DOS equivalent works fine:
    >
    > C:\> echo google.com | nslookup
    > Default Server: dns.erco.x
    > Address: 192.168.1.14
    > [..expected output..]
    >
    > 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.
    >
    > FWIW, I'm using the windows version of python 2.5 from activestate.


    Glad to see you're finally coming into the light Greg! I've used Rush
    in a few different studios over the past couple of years. We even had
    sushi once. :)

    I'm no expert like you, but I think I can point you in the right
    direction. You need os.popen2 which returns a tuple of file-like
    objects. The first pointing to stdin, and the second pointing to
    stdout. Write to stdin, and read from stdout.

    import os
    import sys
    stdin, stdout = os.popen2("nslookup")
    stdin.write("google.com\n")
    stdin.close()

    print stdout.read()
    stdout.close()

    I don't use windows much, but I believe the os.popen functionality is
    being replaced by subprocess.Popen:

    from subprocess import *
    import sys

    p = Popen("nslookup", shell=True, bufsize=1024, stdin=PIPE,
    stdout=PIPE, close_fds=True)
    p.stdin.write("google.com\n")
    p.stdin.close()

    print p.stdout.read()
    p.stdout.close()

    I found these:
    http://pydoc.org/2.4.1/subprocess.html
    http://docs.python.org/lib/module-subprocess.html

    ~Sean DiZazzo
    Curious George
     
    , May 12, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    7
    Views:
    1,314
    Karthik Gurusamy
    Sep 25, 2007
  2. John O'Hagan
    Replies:
    6
    Views:
    371
    John O'Hagan
    Feb 12, 2011
  3. P.S.
    Replies:
    0
    Views:
    351
  4. Miki Tebeka
    Replies:
    2
    Views:
    633
    Chris Angelico
    Apr 8, 2011
  5. Z W
    Replies:
    0
    Views:
    196
Loading...

Share This Page