subprocess.Popen() problem

Discussion in 'Python' started by 7stud, Jun 26, 2007.

  1. 7stud

    7stud Guest

    I have this program:

    mytest.py
    --------------
    myinput = raw_input("Enter input: ")

    if myinput == "hello":
    print "goodbye"
    --------------

    and I want to execute it using subprocess.Popen(). I tried the
    following but it just hangs:

    -----------
    import subprocess

    f = open("/Users/me/2testing/dir1/aaa.txt", "w")
    my_path = "/Users/me/2testing/"
    my_file = "mytest.py"

    p = subprocess.Popen(["python", "mytest.py"], stdin=subprocess.PIPE,
    stdout = f,
    stderr = f,
    cwd = my_path
    )

    f.close()

    p.stdin.write("hello")
    p.wait()

    f.open("/Users/me/2testing/dir1/aaa.txt")
    print f.read()
    ------------
     
    7stud, Jun 26, 2007
    #1
    1. Advertising

  2. 7stud

    Jerry Hill Guest

    On 6/26/07, 7stud <> wrote:
    > p.stdin.write("hello")


    You need to add the linefeed, otherwise your mytest.py process is
    still waiting for you to finish typing. So, use this instead:

    p.stdin.write("hello\n")

    --
    Jerry
     
    Jerry Hill, Jun 26, 2007
    #2
    1. Advertising

  3. 7stud

    Guest

    On Jun 26, 3:38 pm, 7stud <> wrote:
    > I have this program:
    >
    > mytest.py
    > --------------
    > myinput = raw_input("Enter input: ")
    >
    > if myinput == "hello":
    > print "goodbye"
    > --------------
    >
    > and I want to execute it using subprocess.Popen(). I tried the
    > following but it just hangs:
    >
    > -----------
    > import subprocess
    >
    > f = open("/Users/me/2testing/dir1/aaa.txt", "w")
    > my_path = "/Users/me/2testing/"
    > my_file = "mytest.py"
    >
    > p = subprocess.Popen(["python", "mytest.py"], stdin=subprocess.PIPE,
    > stdout = f,
    > stderr = f,
    > cwd = my_path
    > )
    >
    > f.close()
    >
    > p.stdin.write("hello")
    > p.wait()
    >
    > f.open("/Users/me/2testing/dir1/aaa.txt")
    > print f.read()
    > ------------


    Never tried this, but I think you need to use the communicate method
    detailed here:

    http://docs.python.org/dev/lib/node537.html

    If that doesn't work, you might look into using the threading module
    and its methods. Or there could be a flush command that I can't
    find...

    Mike
     
    , Jun 26, 2007
    #3
  4. 7stud

    7stud Guest

    On Jun 26, 3:00 pm, "Jerry Hill" <> wrote:
    > On 6/26/07, 7stud <> wrote:
    >
    > > p.stdin.write("hello")

    >
    > You need to add the linefeed, otherwise your mytest.py process is
    > still waiting for you to finish typing. So, use this instead:
    >
    > p.stdin.write("hello\n")
    >


    Arggh. Of course! Thanks

    >Never tried this, but I think you need to use the communicate method
    >detailed here:


    Yes, communicate() will work too, but communicate reads stdout into a
    string(i.e. into memory), and I don't want to do that. Thanks.
     
    7stud, Jun 26, 2007
    #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. Michele Simionato

    subprocess.Popen

    Michele Simionato, Dec 12, 2004, in forum: Python
    Replies:
    5
    Views:
    17,700
  2. The Cruisemaniac

    Problem with subprocess.Popen()

    The Cruisemaniac, Aug 1, 2007, in forum: Python
    Replies:
    1
    Views:
    482
    O.R.Senthil Kumaran
    Aug 1, 2007
  3. Tim Golden
    Replies:
    0
    Views:
    464
    Tim Golden
    Oct 7, 2007
  4. Mathieu Prevot
    Replies:
    3
    Views:
    567
    Mathieu Prevot
    Jul 7, 2008
  5. File.popen/IO.popen

    , May 20, 2006, in forum: Ruby
    Replies:
    1
    Views:
    244
    Robert Klemme
    May 20, 2006
Loading...

Share This Page