How to get output of exe in python script?

后端 未结 2 406
悲哀的现实
悲哀的现实 2021-02-04 15:09

When I call an external .exe program in Python, how can I get printf output from the .exe application and print it to my Python IDE?

2条回答
  •  梦毁少年i
    2021-02-04 15:42

    I am pretty sure that you are talking about Windows here (based on the phrasing of your question), but in a Unix/Linux (including Mac) environment, the commands module is also available:

    import commands
    
    ( stat, output ) = commands.getstatusoutput( "somecommand" )
    
    if( stat == 0 ):
        print "Command succeeded, here is the output: %s" % output
    else:
        print "Command failed, here is the output: %s" % output
    

    The commands module provides an extremely simple interface to run commands and get the status (return code) and the output (reads from stdout and stderr). Optionally, you can get just status or just output by calling commands.getstatus() or commands.getoutput() respectively.

提交回复
热议问题