Reputation: 11
I am trying to build a python GUI app which needs to run bash scripts and capture their output. Most of the commands used in these scripts require further inputs at run time, as they deal with network and remote systems.
Upvotes: 1
Views: 2391
Reputation: 1299
Python's subprocess module allows one to easily run external programs, while providing various options for convenience and customizability.
To run simple programs that do not require interaction, the functions call()
, check_call()
, and check_output()
(omitting arguments) are very useful.
For more complex use cases, where interaction with the running program is required, Popen Objects can be used, where you can customize input/output pipes, as well as many other options - the aformentioned functions are wrappers around these objects. You can interact with a running process through the provided methods poll()
, wait()
, communicate()
, etc.
As well, if communicate()
doesn't work for your use case, you can get the file descriptors of the PIPEs via Popen.stdin
, Popen.stdout
, and Popen.stderr
, and interact with them directly with select. I prefer Polling Objects :)
Upvotes: 1