469,646 Members | 1,150 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

Having trouble converting popen2 to subprocess

Here's a c routine that prints a single line :

#include <stdio.h>
main()
{
printf ("Hello World!\n");
}

And now the Python program (called 'po.py') that uses 'popen2' :

import popen2
(fin, fout) = popen2.popen2(r'c:\home\hw.exe', -1, 't')
print fin.readline()
fin.close()
fout.close()

When this is run it properly outputs the one line from the c routine :

C:\>python c:\python\po.py
Hello World!

Now here is my attempt to use the 'subprocess' module :

from subprocess import *
p = Popen(r'c:\home\hw.exe', bufsize=-1, stdin=PIPE, stdout=PIPE,
universal_newlines=True)
fin = p.stdin
print fin.readline()
fin.close()

When this is run, I get no output :

C:\>python c:\python\sp.py
C:\>

As you can see, I get no exception.

I've tried various combinations of the Popen arguments with no joy.

The platform is Windows XP Pro, so I did not try things like
'close_fds'.

What am I missing ?

Daniel Klein
Nov 18 '06 #1
3 1967
tom
Daniel Klein wrote:
Here's a c routine that prints a single line :

#include <stdio.h>
main()
{
printf ("Hello World!\n");
}

And now the Python program (called 'po.py') that uses 'popen2' :

import popen2
(fin, fout) = popen2.popen2(r'c:\home\hw.exe', -1, 't')
print fin.readline()
fin.close()
fout.close()

When this is run it properly outputs the one line from the c routine :

C:\>python c:\python\po.py
Hello World!

Now here is my attempt to use the 'subprocess' module :

from subprocess import *
p = Popen(r'c:\home\hw.exe', bufsize=-1, stdin=PIPE, stdout=PIPE,
universal_newlines=True)
fin = p.stdin
print fin.readline()
fin.close()

When this is run, I get no output :

C:\>python c:\python\sp.py
C:\>

As you can see, I get no exception.

I've tried various combinations of the Popen arguments with no joy.

The platform is Windows XP Pro, so I did not try things like
'close_fds'.

What am I missing ?

Daniel Klein
subprocess will actually execute as a subprocess, so you have to wait
for the command to finish before you look at the stdout. Advantages of
this being that you can interfere with stdin/out whilst the program is
running. I believe .wait will be what you want, although i haven't look
at the docstring, so double check.
Nov 18 '06 #2
Daniel Klein wrote:
Now here is my attempt to use the 'subprocess' module :

from subprocess import *
p = Popen(r'c:\home\hw.exe', bufsize=-1, stdin=PIPE, stdout=PIPE,
universal_newlines=True)
fin = p.stdin
p.stdin is the *other* process' stdin. if you want to read things it
prints, read from p.stdout instead.
print fin.readline()
fin.close()
</F>

Nov 18 '06 #3

Thanks /F, that was it.

Dan

On Sat, 18 Nov 2006 15:03:30 +0100, Fredrik Lundh
<fr*****@pythonware.comwrote:

[snip]
>p.stdin is the *other* process' stdin. if you want to read things it
prints, read from p.stdout instead.
>print fin.readline()
fin.close()

</F>
Nov 18 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Diez B. Roggisch | last post: by
4 posts views Thread by P | last post: by
4 posts views Thread by Rembrandt Q Einstein | last post: by
3 posts views Thread by jb | last post: by
1 post views Thread by Daniel Klein | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.