Advantages of subprocess over os.system

前端 未结 2 997
悲哀的现实
悲哀的现实 2020-12-09 17:12

I have recently came across a few posts on stack overflow saying that subprocess is much better than os.system, however I am having difficulty finding the exact advantages.

2条回答
  •  无人及你
    2020-12-09 17:43

    There are many reasons, but the main reason is mentioned directly in the docstring:

    >>> os.system.__doc__
    'Execute the command in a subshell.'
    

    For almost all cases where you need a subprocess, it is undesirable to spawn a subshell. This is unnecessary and wasteful, it adds an extra layer of complexity, and introduces several new vulnerabilities and failure modes. Using subprocess module cuts out the middleman.

提交回复
热议问题