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?

2
  • A better duplicate of stackoverflow.com/questions/89228/… Commented Nov 22, 2017 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. Commented Aug 2, 2018 at 20:44

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.