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

    Join Date
    Feb 2005
    Posts
    6
    Rep Power
    0

    Trying to catch output from another executable


    I am trying to receive the output (strings) from executing one python file from another python file, but I am having difficulty figurring this out. I have used eval(), but that doesn't seem to work for every line in the file. I tried execfile(), but that runs the entire program without getting any output back.

    For example, I have 2 files a.py and b.py, and a.py needs to run b.py. My a.py program needs to know what the output of b.py gives.

    If anyone can help me out with this, I'd appreciate it.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    If the second .py file is a part of your program, then it sounds like you want to make it into a module. You could do something like:

    wrap all the code in a function, so b.py goes from:

    Code:
    i = 1
    print i+1
    to

    Code:
    def b():
        i = 1
        return i+1
    Then from a you can import it:

    Code:
    import b
    
    print b.b()

    If you actually need to run it as a separate process, try looking into the commands and/or os.popen features on python.org documentation.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    6
    Rep Power
    0
    Thanks sfb, however I do need to run it as a separate process. I actually did just find the os.popen() and popen2() commands. However, the documentation seems a little vague (at least to me), and I have not been to get it to work.

    I am assuming that the only way to do this is to get the other process (process b) to somehow print out to a text file, and then have process a read this file? I can't seem to get the process b to print to a file however.
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    The best way to do this is to use the subprocess module; if your running Python 2.4+ on your system. If not then you should look at os.popen() or the popen2 module.

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

    All of these start the Python program in a separate process, and allow you to capture the output.

    subprocess is a little more complicated but is recommended to the alternatives. If you need an example let me know, but I don't have Python 2.4 on this computer .

    Hope this helps,

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

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

    Join Date
    Feb 2005
    Posts
    6
    Rep Power
    0
    Mark,

    I am running Python 2.4, however, I would appreciate an example of how to use popen2() to capture the output. I don't quite understand how the 2 return values of popen2() work, the child_stdout and child_stdin.

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

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Here’s an example that uses os.popen() to capture the output from a python program (a.py). This program simply prints 'Hello Subworld!’.

    Code:
    >>> stdo = os.popen('python a.py')
    >>> stdo.read()
    'Hello Subworld!\n'
    >>>
    os.popen() returns a file() object which you can use to read the output from the subprocess started by the function. This is fine if you only need access to stdout.

    Likewise: the functions in the popen2 module work in the same way but return a different number of values i.e. popen2() returns a file() object for stdin and stdout while popen3 returns one for stdin, stdout and stderr.

    Make sense?

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

  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    6
    Rep Power
    0
    Mark,

    Thanks a bunch, it's so simple that I could laugh at myself for not seeing it before. I appreciate your help, have a good one!
  14. #8
  15. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    I know this is completely off-topic. But Terenas, have you ever had the account {TERENAS)~{GOD} on battle.net?
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2005
    Posts
    6
    Rep Power
    0
    Yegg,

    Although I have played on battle.net before many times, that was not my account. Terenas is the server I play on in World of Warcraft, and it was the first thing that came to mind when I created my account, as I was in a hurry.
  18. #10
  19. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    155
    Ok, thanks.

IMN logo majestic logo threadwatch logo seochat tools logo