Difference between .Popen and os.system subprocess

What is the difference between subprocess.Popen () and os.system ()?

+66
python subprocess
Jan 27 2018-11-11T00:
source share
4 answers

If you look at the subprocess section in Python docs , you'll notice that there is an example of how to replace os.system() with subprocess.Popen() :

 sts = os.system("mycmd" + " myarg") 

... does the same as ...

 sts = Popen("mycmd" + " myarg", shell=True).wait() 

The "improved" code looks more complex, but better, because as soon as you know subprocess.Popen() , you don't need anything else. subprocess.Popen() replaces several other tools ( os.system() is just one of them) that were scattered across three other Python modules.

If this helps, think of subprocess.Popen() as a very flexible os.system() .

+52
Jan 27 2018-11-11T00:
source share

subprocess.Popen () is a strict superset of os.system ().

+20
27 '11 at 6:17
source share

The subprocess is based on popen2, and as such has a number of advantages - there is a complete list in

+18
Jan 27 2018-11-11T00:
source share

os.system is equivalent to the Unix system command, while the subprocess was a helper module designed to provide many of the features provided by the Popen commands with a simpler and more manageable interface. They were similar to the Unix Popen team.

 system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed 

where as

 The popen() function opens a process by creating a pipe, forking, and invoking the shell. 

If you are thinking which one to use, then use the subprocess specifically because you have all the options to execute, plus additional control over the process.

+16
Jan 27 '11 at 7:07
source share



All Articles