subprocess.Popen() problem

kyosohma at gmail.com kyosohma at gmail.com
Tue Jun 26 17:03:38 EDT 2007


On Jun 26, 3:38 pm, 7stud <bbxx789_0... at yahoo.com> 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





More information about the Python-list mailing list