472,378 Members | 1,328 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,378 software developers and data experts.

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 2029
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Diez B. Roggisch | last post by:
Hi, I'm using the popen2 module to communicate with the crm114 text classificator. First I used to create a subprocess, feed the text to classify to it, close the crm's stdin file and read the...
4
by: P | last post by:
I've written a couple of apps that required running a command and grabbing the output, and I've found the existing interfaces problematic for this. I think the proliferation of functions and...
4
by: Rembrandt Q Einstein | last post by:
I am running an external command and I need to know a) when it is done and b) what it wrote to both stdout and stderr. After a little searching, I found the popen2 module and used the Popen3...
3
by: jb | last post by:
Hi there: I need help with popen2 usage. I am coding on Windows 2000 environment and I am basically trying to run command line executable program that accepts command line arguments from user....
3
by: mikem76 | last post by:
How do I automatically redirect stdout and stderr when using os.popen2 to start a long running process. If the process prints a lot of stuff to stdout it will eventually stop because it runs out...
1
by: mikem76 | last post by:
Is there a way to get the process id when starting a process using os.popen2 or os.popen3 on linux? Mike
5
by: metaperl.etc | last post by:
The following program does not work if you uncomment #lis = + list(args) Evidently Python is opting for the nullary constructor list() as opposed to the other one which takes a sequence....
1
by: Daniel Klein | last post by:
I have a few Python programs that use popen2, and they work quite nicely and dependably, so I don't really have any reason to change them to use the new subprocess module...unless of course there...
2
by: Michael George Lerner | last post by:
Hi, (Python 2.5, OS X 10.4.10) I have a program called pdb2pqr on my system. It is installed so that "pdb2pqr" is in my path and looks like: #\!/bin/zsh -f /sw/share/pdb2pqr/pdb2pqr.py "$@"...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...
0
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.