December 12th, 2004, 09:13 PM
why is my fileno -1?
we have lis.py, which does this
and we have the following
If you run lis.py by itself, you got 0 and 1 (for stdin and out, respectively). But when I create a pipe to lis.py, we get -1 and 1 (for its stdin and stdout). A fileno of -1 is invalid.
pin, pout = os.popen2("lis.py", 't', -1)
for line in pout.readlines():
Why is the new stdin -1??? I wish to talk to lis.py and read the result, in order to verify IPC done with popen.
December 13th, 2004, 01:46 PM
I did a few quick tests on the code but ended up with the expected 0 and 1 each time. Here's the session, hopefully you'll be able to spot something I didn't .
Also note that you don't have to pass the second and third arguments to popen2().
If your using Python 2.4 for this you might also be interested in the subprocess module [recommended]. What version are you using anyway?
>>> stdi, stdo = os.popen2('/home/Netytan/Desktop/lis.py', 't', -1)
<open file '<fdopen>', mode 'w' at 0x40281650>
<open file '<fdopen>', mode 'r' at 0x40281698>
>>> stdi, stdo = os.popen2('/home/Netytan/Desktop/lis.py')
>>> for line in stdo:
... print line,
December 13th, 2004, 03:59 PM
netytan - Are you using linux to run this example? I've verified that this works on another linux machine.
It was also suggested that since .py aren't really executables in the sense that popen expects that might be why popen returns an invalid stdin. doing something like C:\...\python lis.py doesn't work either. I'm not sure what the problem is. I'm using w2k with py2.3
December 14th, 2004, 03:16 AM
You’re right; I ran the example on Susu 9.1 so that would explain it. It could just be a glitch in the way popen2() works on Windows but I'd lean away from that assumption for now. Anyway I'll give it a go tonight.
Until then you could try doing 'python C:\...\lis.py' -- assuming that python is on your PATH variable (or you need to do 'C:\Python23\python C:\...\lis.py'.)
Let me know if it works,
December 14th, 2004, 01:33 PM
Ah... it works! It goes like this:
cmd3 = 'python lis.py'
and from there, using cmd3, I get a valid pipe to stdin. Thanks a lot.
December 15th, 2004, 03:12 AM
Glad you got it working , anyway here's my test session on Windows XP for those of you who are interested.
As you can see It seems to work as it does on *nix assuming that you invoke python in just the right way or you get nothing useful .
>>> stdi, stdo = os.popen2('python "C:\Documents and Settings\Mark\Desktop\lis.py"')