Just to point out a common misconception, you should avoid Popen
always when you can. To quote the documentation,
The recommended approach
to invoking subprocesses is
to use the run()
function
for all use cases
it can handle.
For more advanced
use cases, the underlying
Popen
interface
can be used directly.
If you just want to run a subprocess and wait for it to finish, that's a single line of code with subprocess.run
or its legacy siblings subprocess.call
and subprocess.check_output
, and you don't need to copy/paste and/or understand the intricacies of the communicate
and wait
etc methods required around the low-level Popen
object.
import subprocess
proc = subprocess.run(
[openRTSP] + opts.split(),
capture_output=True,
# avoid having to explicitly encode
text=True)
data = proc.stdout
result = proc.returncode
If you don't want to capture the output from the process, maybe replace capture_output=True
with stdout=subprocess.DEVNULL
(and perhaps similarly for stderr
); in the absence of either, the output will simply be displayed to the user, outside of Python's control.
Also, unless your options in opts
are completely trivial, generally replace the regular string split()
here with shlex.split()
which understands how to cope with quoted strings.