newbie: how to capture/write to stdio on NT

Discussion in 'Python' started by Sholtz, May 22, 2004.

  1. Sholtz

    Sholtz Guest

    Hi,

    I am trying to figure out how to 'control' the input & output using
    popen/popen2 etc on Python 2.3
    I have found examples for unix such as the one below but I can't get it to
    work on Windows NT.

    If I use the os.popen module I can read OR write not both.

    Anybody have any ideas?

    Regards,

    Sholto.

    # Open command in a pipe
    # which reads from stdin and writes to stdout

    import popen2
    pipe = popen2.Popen4("wc -l") # Unix command
    pipe.tochild.write("line 1\nline 2\nline 3\n")
    pipe.tochild.close()
    output = pipe.fromchild.read()
    Sholtz, May 22, 2004
    #1
    1. Advertising

  2. "Sholtz" <> wrote in message news:<jLMrc.5349$>...
    > Hi,
    >
    > I am trying to figure out how to 'control' the input & output using
    > popen/popen2 etc on Python 2.3
    > I have found examples for unix such as the one below but I can't get it to
    > work on Windows NT.
    >
    > If I use the os.popen module I can read OR write not both.
    >
    > Anybody have any ideas?
    >
    > Regards,
    >
    > Sholto.
    >

    popen() at least used to not work reliably under Windows, not sure if
    that's the current status. There apparently were problems with the
    POSIX emulation and the fact that the Windows methods for creating
    processes are somewhat different. You might look into the
    CreateProcess() system call.
    A. Lloyd Flanagan, May 25, 2004
    #2
    1. Advertising

  3. Sholtz

    Arnold Filip Guest

    Sholtz wrote:
    > Hi,
    >
    > I am trying to figure out how to 'control' the input & output using
    > popen/popen2 etc on Python 2.3
    > I have found examples for unix such as the one below but I can't get it to
    > work on Windows NT.
    >
    > If I use the os.popen module I can read OR write not both.
    >
    > Anybody have any ideas?
    >
    > Regards,
    >
    > Sholto.
    >
    > # Open command in a pipe
    > # which reads from stdin and writes to stdout
    >
    > import popen2
    > pipe = popen2.Popen4("wc -l") # Unix command
    > pipe.tochild.write("line 1\nline 2\nline 3\n")
    > pipe.tochild.close()
    > output = pipe.fromchild.read()
    >
    >


    I use the process.py module for this purpose:
    http://starship.python.net/crew/tmick/

    Example (works on windows):

    import process
    import sys

    p = process.ProcessProxy( sys.argv[1:],
    stdout=sys.stderr,
    stderr=sys.stderr )
    p.wait()

    Cheers
    Arnold
    Arnold Filip, May 26, 2004
    #3
  4. Sholtz

    Duncan Booth Guest

    "Sholtz" <> wrote in
    news:jLMrc.5349$:

    > I am trying to figure out how to 'control' the input & output using
    > popen/popen2 etc on Python 2.3
    > I have found examples for unix such as the one below but I can't get
    > it to work on Windows NT.
    >
    > If I use the os.popen module I can read OR write not both.
    >
    >
    > # Open command in a pipe
    > # which reads from stdin and writes to stdout
    >
    > import popen2
    > pipe = popen2.Popen4("wc -l") # Unix command
    > pipe.tochild.write("line 1\nline 2\nline 3\n")
    > pipe.tochild.close()
    > output = pipe.fromchild.read()
    >

    As the documentation for popen2 says, the methods such as popen4 exist on
    windows, but the classes don't. So you have to use:

    >>> import popen2
    >>> pipeout, pipein = popen2.popen4("more")
    >>> pipein.write("line 1\nline 2\nline 3\n")
    >>> pipein.close()
    >>> print pipeout.read()

    line 1
    line 2
    line 3


    >>>


    Also be sure to read the section of the documentation about flow control
    issues. If there can ever be more than 4k of output waiting to be read your
    code will deadlock. If the program is trying to read input, then you won't
    get any output until it has read enough or you close the input.

    The simplest thing is to always be sure to read pipeout on a different
    thread than you use to write to pipein. You cannot use 'select' on windows
    pipes.
    Duncan Booth, May 26, 2004
    #4
    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. Max
    Replies:
    7
    Views:
    9,076
  2. Robert Maas, see http://tinyurl.com/uh3t

    How to disable stdio echoplex from java code (unix specific)?

    Robert Maas, see http://tinyurl.com/uh3t, Jun 7, 2005, in forum: Java
    Replies:
    0
    Views:
    387
    Robert Maas, see http://tinyurl.com/uh3t
    Jun 7, 2005
  3. Vicent

    iostream or stdio

    Vicent, Aug 25, 2003, in forum: C++
    Replies:
    2
    Views:
    1,716
  4. paul calvert
    Replies:
    6
    Views:
    2,141
  5. Replies:
    18
    Views:
    1,877
    crazzybugger
    Oct 22, 2006
Loading...

Share This Page