Here's where we start to go from the built‑in functions to
dipping our toe into the modules and libraries Python can
offer to expand the power of the tool.
The subprocess module is part of the standard library, but needs to be imported
in order to take advantage of the functions inside of it.
The subprocess module allows you to kick off new processes or
applications and connect to different input and output pipes.
The run function was first introduced in Python 3.5.
It is a simple and easy way to run your basic commands.
Simply pass the command you want to run,
for example the ls on the Linux box, the command will
open a shell and pass the command value,
then return the script and move on to the next line.
Each of the functions here executes a subprocess slightly different.
I would recommend as you start to build out your script, you start
with a basic run or call command and see if that handles the call
the way you expect, and work from there.
Each of the functions process the command slightly differently,
but all handle the same set of arguments.
The stdin, stdout, stderr are what you expect, details on the command's input,
standard output, and standard error handling. The shell argument,
which defaults the false, specifies whether to use
the shell to execute the command,
which means the command must be formatted the same
as you would at the shell prompt.
This option gives you the access to additional
functionality available in the shell.
The cwd, current working directory argument,
changes the working directory,
allowing you to open programs and leave output in a
specific directory when finished.
Now there are other arguments,
but those are not as common across. A distinct difference
between all the functions is how they handle the timing of
the commands they are running.
Do they kick off immediately, move to the next line in the
script regardless of the outcome of the subprocess or do
they wait before moving on?
In some cases, the same function will behave differently based on
what commander application is being opened.