Will it continue the code after it's run? Or will it stop at that line until the script is done?
Python allows you to execute shell commands, which you can use to start other programs or better manage shell scripts that you use for automation. Depending on our use case, we can use os. system() , subprocess. run() or subprocess.
Is there any way to execute the bash commands and scripts in Python? Yeah, Python has a built-in module called subprocess which is used to execute the commands and scripts inside Python scripts.
Using subprocess.call is the easiest way. It will not return until the executed program has terminated. Have a look at the other methods of the subprocess module if you need different behaviour.
import os
os.system('./script.sh')
python script won't stop until sh is finished
You can use os.system
or subprocess.Popen
or subprocess.call
but when using subprocess methods make sure you use shell=True
. And executing it via system call in all these methods is blocking. The python script will complete and then go the next step.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With