Will it continue the code after it's run? Or will it stop at that line until the script is done?
3 Answers
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.
-
subprocess.call
was deprecated in 3.5:subprocess.run
should be used instead.– wordbugJan 22, 2019 at 9:09
import os
os.system('./script.sh')
python script won't stop until sh is finished
-
"The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function." - for example using subprocess.call() will not execute a shell to run whatever you run so you won't have to take care of argument injection if you pass any dynamically generated arguments to your program. Jan 28, 2011 at 9:41
-
1You can also suggest subprocess.Popen -- this could allow you to control the execution, wait for it, terminate, etc. But as practice shows os.system often provides all the functionality you need. Jan 28, 2011 at 9:43
-
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.
-
3No need to use shell=True.. Shellscripts with a shebang line (or even invoking the shell with the script as an argument) can be executed fine with exec()-style functions. Jan 28, 2011 at 9:45