Difference between subprocess.Popen and os.system

PythonSubprocessSystem

Python Problem Overview


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

Python Solutions


Solution 1 - Python

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

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

...does the same thing as...

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

The "improved" code looks more complicated, but it's better because once you know subprocess.Popen(), you don't need anything else. subprocess.Popen() replaces several other tools (os.system() is just one of those) that were scattered throughout three other Python modules.

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

Solution 2 - Python

subprocess.Popen() is strict super-set of os.system().

Solution 3 - Python

os.system is equivalent to Unix system command, while subprocess was a helper module created to provide many of the facilities provided by the Popen commands with an easier and controllable interface. Those were designed similar to the Unix Popen command.

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

Whereas:

> 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 subprocess definitely because you have all the facilities for execution, plus additional control over the process.

Solution 4 - Python

Subprocess is based on popen2, and as such has a number of advantages - there's a full list in the PEP here, but some are:

  • using pipe in the shell
  • better newline support
  • better handling of exceptions

Solution 5 - Python

When running python (cpython) on windows the <built-in function system> os.system will execute under the curtains _wsystem while if you're using a non-windows os, it'll use system.

On contrary, Popen should use CreateProcess on windows and _posixsubprocess.fork_exec in posix-based operating-systems.

That said, an important piece of advice comes from os.system docs, which says:

> The subprocess module provides more powerful facilities for spawning > new processes and retrieving their results; using that module is > preferable to using this function. See the Replacing Older Functions > with the subprocess Module section in the subprocess documentation for > some helpful recipes.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArovitView Question on Stackoverflow
Solution 1 - PythonJacob MarbleView Answer on Stackoverflow
Solution 2 - PythonJan HudecView Answer on Stackoverflow
Solution 3 - PythonSenthil KumaranView Answer on Stackoverflow
Solution 4 - PythonAndy MikulaView Answer on Stackoverflow
Solution 5 - PythonBPLView Answer on Stackoverflow