#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    17
    Rep Power
    0

    why is my fileno -1?


    we have lis.py, which does this
    Code:
    import sys
    
    print sys.stdin.fileno()
    print sys.stdout.fileno()
    and we have the following
    Code:
    import os
    
    pin, pout = os.popen2("lis.py", 't', -1)
    
    for line in pout.readlines():
        print line
    
    pin.close()
    pout.close()
    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.

    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.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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().

    Code:
    >>> stdi, stdo = os.popen2('/home/Netytan/Desktop/lis.py', 't', -1)
    >>> stdi
    <open file '<fdopen>', mode 'w' at 0x40281650>
    >>> stdo
    <open file '<fdopen>', mode 'r' at 0x40281698>
    >>> stdo.read()
    '0\n1\n'
    >>> stdi, stdo = os.popen2('/home/Netytan/Desktop/lis.py')
    >>> for line in stdo:
    ...     print line,
    ...
    0
    1
    >>>
    If your using Python 2.4 for this you might also be interested in the subprocess module [recommended]. What version are you using anyway?

    http://www.python.org/doc/2.4/lib/module-subprocess.html

    Have fun,

    Mark.
    programming language development: www.netytan.com – Hula

  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    17
    Rep Power
    0
    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
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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,

    Mark.
    programming language development: www.netytan.com – Hula

  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    17
    Rep Power
    0
    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.

    cheers
    ktpr
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Glad you got it working , anyway here's my test session on Windows XP for those of you who are interested.

    Code:
    >>> stdi, stdo = os.popen2('python "C:\Documents and Settings\Mark\Desktop\lis.py"')
    >>> stdo.read()
    '0\n1\n'
    >>>
    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 .

    Mark.
    programming language development: www.netytan.com – Hula


IMN logo majestic logo threadwatch logo seochat tools logo