Fix Python – Difference between subprocess.Popen and os.system

Question

Asked By – Arovit

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

Now we will see solution for issue: Difference between subprocess.Popen and os.system


Answer

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

This question is answered By – Jacob Marble

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0