3

I read this question in which the asker is having a specific problem with running bash in Python using code like this:

os.system(bashCommand)

The top two answers simply say: "use subprocess" (instead of os.system), and give a quick code example.

What is the proper way to run bash commands with Python and why? To me, it seems that os.system is a good option, being designed for this type of thing, and it is simply a fluke that the asker of the other question couldn't accomplish correct functionality with that package. But, is it "the true Pythonic way" to use subprocess? Or in other words, what is the difference between os.system and subprocess?

Joseph Hansen
  • 12,665
  • 8
  • 50
  • 68
  • A better duplicate of https://stackoverflow.com/questions/89228/calling-an-external-command-in-python?rq=1 – Nabin Nov 22 '17 at 14:01
  • @JosphHansen It's worth noting that the `system()` function in C -- which `os.system()` wraps without changes -- is very, *very* old: It was designed back before security concerns were a thing, for example, so there wasn't any attention paid to keeping data out-of-band from code. It's not a good API in *any* language if one is concerned about handling hostile data safely. – Charles Duffy Aug 02 '18 at 20:44

1 Answers1

1

Subprocess gives you much more control over what's going on.

For example, you can redirect output to pipe it to your program like that:

process = subprocess.Popen(['ls', '-a'], stdout=subprocess.PIPE)
out, err = process.communicate()

(Example from python getoutput() equivalent in subprocess)

If you used system() you'd have to do redirection of input, saving it to file and weird things like that.


In documentation for os.system (https://docs.python.org/2/library/os.html#os.system) it is said that:

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.

Community
  • 1
  • 1
Dmitry Torba
  • 3,004
  • 1
  • 14
  • 24