194

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

Xantium
  • 11,201
  • 10
  • 62
  • 89
Arovit
  • 3,579
  • 5
  • 20
  • 24

5 Answers5

157

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().

Jacob Marble
  • 28,555
  • 22
  • 67
  • 78
  • 1
    You can also use `sts = p.wait()` instead of the last line. – Sven Marnach Jan 27 '11 at 08:10
  • 1
    @JacobMarble so suppose I am calling a selenium scraping script from another python script, which of these would allow me to complete the scraping script and then and only then execute the next line of code? As in, my scraping should complete before the execution can continue. – praxmon Mar 27 '15 at 13:15
  • Maybe also mention http://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess to point out that you ought to avoid the `shell=True` if you can. Having this option is one of the benefits of `subprocess.Popen()` and its helper functions. – tripleee Feb 16 '17 at 05:50
  • 1
    @PrakharMohanSrivastava If you want to block the flow and wait for command to complete its work check out: https://stackoverflow.com/a/2837319/5252192 – Amir Hossein Baghernezad Aug 01 '17 at 06:59
  • @PrakharMohanSrivastava, in general both `os.system` and `subprocess.call/popen/run` does block/wait for the command to finish: https://stackoverflow.com/a/14059648/4752883 – alpha_989 Mar 10 '18 at 00:54
  • (small fact) In 2022 if you take the same [documentation](https://docs.python.org/3/library/subprocess.html#replacing-os-system), `sts = os.system("mycmd" + " myarg")` becomes now `retcode = call("mycmd" + " myarg", shell=True)` –  Jan 13 '22 at 14:55
81

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

Xantium
  • 11,201
  • 10
  • 62
  • 89
Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
38

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.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Senthil Kumaran
  • 54,681
  • 14
  • 94
  • 131
  • okay..then is there any way i can execute both os.system and popen in windows env ? i am a newbie and need to dig deeper.. – Arovit Jan 27 '11 at 08:15
  • Yes, you can use both os.system and subprocess in Windows Environment. Have a look at basic tutorials from effbot.org (Python Standard Library online book) – Senthil Kumaran Jan 27 '11 at 08:40
26

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
Andy Mikula
  • 16,796
  • 4
  • 32
  • 39
9

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.

BPL
  • 9,632
  • 9
  • 59
  • 117