473,383 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 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 2072
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 "$@"...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.