Python 的 subprocess.call 和 subprocess.run 有什么区别 [英] What's the difference between Python's subprocess.call and subprocess.run

查看:305
本文介绍了Python 的 subprocess.call 和 subprocess.run 有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图了解 subprocess.callsubprocess.run 之间的区别是什么.我知道最后一个是 Python 3.5 上的新内容,两者都基于 subprocess.Popen,但我还无法理解其中的区别.

I've been trying to understand for a while now what's the difference between subprocess.call and subprocess.run. I know the last one is new on Python 3.5 and both are based on subprocess.Popen, but I'm not able to understand the difference yet.

推荐答案

subprocess.call() 明确提到:

相当于:run(...).returncode(除了不支持inputcheck参数)

It is equivalent to: run(...).returncode (except that the input and check parameters are not supported)

作为 Python 3.5 的子流程文档 说:

在 Python 3.5 之前,这三个函数(即 .call().check_call().check_output())包括子进程的高级 API.您现在可以在许多情况下使用 run(),但许多现有代码会调用这些函数.

Prior to Python 3.5, these three functions (i.e. .call(), .check_call(), .check_output()) comprised the high level API to subprocess. You can now use run() in many cases, but lots of existing code calls these functions.

<小时>

通常的做法是,当某些功能被替换时,它们不会立即被弃用,但对于某些版本有一个支持窗口.这有助于防止在升级语言版本时破坏旧代码.我不知道 .call() 将来是否会被替换.但根据文档,我知道它们几乎相同.


It is a common practice that when some functions are replaced, they are not instantly deprecated but there is a support window for them for some versions. This helps in preventing the breakage of older code when the language version is upgraded. I do not know whether .call() is going to be replaced in the future or not. But based on the document, what I know is that they are pretty much same.

这篇关于Python 的 subprocess.call 和 subprocess.run 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆