subprocess.Popen和os.system之间的区别

问题:subprocess.Popen和os.system之间的区别

subprocess.Popen()和之间有什么区别os.system()

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


回答 0

如果您查看Python文档subprocess部分,您会注意到有一个示例如何替换os.system()subprocess.Popen()

sts = os.system("mycmd" + " myarg")

…与…具有相同的作用

sts = Popen("mycmd" + " myarg", shell=True).wait()

“改进的”代码看起来更复杂,但是更好,因为一旦知道了subprocess.Popen(),就不需要其他任何东西。subprocess.Popen()替换了os.system()分散在其他三个Python模块中的其他几个工具(仅是其中的一种)。

如果有帮助,可以认为它subprocess.Popen()是非常灵活的os.system()

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


回答 1

subprocess.Popen()是的严格超集os.system()

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


回答 2

os.system等效于Unix system命令,同时subprocess创建了一个帮助程序模块,以提供Popen命令提供的许多功能,并提供一个更容易控制的界面。它们的设计类似于Unix Popen命令。

system()通过调用执行命令中指定的命令/bin/sh -c command,命令完成后返回

鉴于:

popen()函数通过创建管道,派生和调用外壳程序来打开进程。

如果您正在考虑使用哪一个,那么subprocess一定要使用它,因为您具有执行的所有功能以及对该过程的额外控制。

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.


回答 3

子是基于popen2,因此具有很多优点-有在一个完整列表在这里PEP,但有些是:

  • 在外壳中使用管道
  • 更好的换行支持
  • 更好地处理异常

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

回答 4

当运行在Windows上的Python(CPython的)的<built-in function system> 使用os.system将窗帘下执行_wsystem而如果你使用非Windows操作系统,它会利用系统

相反,Popen应该在Windows上使用CreateProcess,在基于posix的操作系统中使用_posixsubprocess.fork_exec

就是说,重要的建议来自os.system docs,它说:

子流程模块提供了更强大的功能来生成新流程并检索其结果。使用该模块优于使用此功能。有关一些有用的食谱,请参见子过程文档中的用子过程模块替换较早的功能部分。

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.