Python subprocess.Popen 和 os.system 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4813238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 17:39:01  来源:igfitidea点击:

Difference between subprocess.Popen and os.system

pythonsubprocesssystem

提问by Arovit

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

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

采纳答案by Jacob Marble

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

如果检查出Python文档的子部分,你会发现有一个如何来代替的例子os.system()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.

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

If it helps, think of subprocess.Popen()as a very flexible os.system().

如果有帮助,可以将其subprocess.Popen()视为非常灵活的os.system().

回答by Andy Mikula

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:

子进程基于 popen2,因此具有许多优点 - 此处的PEP 中有完整列表,但有些是:

  • using pipe in the shell
  • better newline support
  • better handling of exceptions
  • 在外壳中使用管道
  • 更好的换行支持
  • 更好地处理异常

回答by Jan Hudec

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

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

回答by Senthil Kumaran

os.system is equivalent to Unix systemcommand, 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 Popencommand.

os.system 相当于 Unix系统命令,而subprocess 是一个帮助模块,旨在提供Popen命令提供的许多功能,以及一个更容易和可控的界面。这些设计类似于Unix Popen命令。

system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed

where as

然而

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 subprocessdefinitely because you you have all facilities for execution, plus additional control over the process.

如果您正在考虑使用哪个,那么绝对使用子流程,因为您拥有所有执行设施,以及对流程的额外控制。

回答by BPL

When running python (cpython) on windows the <built-in function system>os.systemwill execute under the curtains _wsystemwhile if you're using a non-windows os, it'll use system.

在 Windows 上运行 python (cpython) 时,<built-in function system>os.system将在窗帘_wsystem下执行,而如果您使用的是非 Windows 操作系统,它将使用system

On contrary, Popen should use CreateProcesson windows and _posixsubprocess.fork_execin posix-based operating-systems.

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

That said, an important piece of advice comes from os.systemdocs, which says:

也就是说,一条重要的建议来自os.systemdocs,它说:

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.

subprocess 模块提供了更强大的工具来生成新进程并检索它们的结果;使用该模块比使用此功能更可取。有关一些有用的秘诀,请参阅子流程文档中的用子流程模块替换旧函数部分。