Running Bash commands in Python
To somewhat expand on the earlier answers here, there are a number of details which are commonly overlooked.
- Prefer
subprocess.run()oversubprocess.check_call()and friends oversubprocess.call()oversubprocess.Popen()overos.system()overos.popen() - Understand and probably use
text=True, akauniversal_newlines=True. - Understand the meaning of
shell=Trueorshell=Falseand how it changes quoting and the availability of shell conveniences. - Understand differences between
shand Bash - Understand how a subprocess is separate from its parent, and generally cannot change the parent.
- Avoid running the Python interpreter as a subprocess of Python.