Python subprocess.call 和 subprocess.Popen 给我不同的输出

标签 python subprocess

使用 subprocess.call 时,输出符合预期。

result = subprocess.call([securefx, '/NoPrompt', '/Q', '/RetryCount', retries, 
              '/RetryDelay', '1', '/Log', sfxLogFile, '/List', '/S', session])

打印结果会输出类似 -533428 或 0

但是当我运行的时候

args = [securefx, '/NoPrompt', '/Q', '/RetryCount', retries, '/RetryDelay', '1', 
       '/Log', sfxLogFile, '/List', '/S', session]
process = subprocess.Popen(args, stdout=subprocess.PIPE)
result = process.communicate()[0]

并打印结果,我得到空白或 stderr = "None"。

为什么不同?即使通话正常,我仍在尝试 Popen 的原因是:Python subprocess.call on sfxcl.exe not working from Windows 2003 Task Scheduler <- 我想我会试一试...

最佳答案

subprocess.Popen 返回 Popen object ,您可以使用它与进程通信并获取输出,但是 subprocess.call 只会返回进程的返回码:

subprocess.call(*popenargs, **kwargs)
  Run command with arguments. Wait for command to complete, then return the returncode attribute.

So subprocess.call is basically equivalent to the following code, and only exists for convenience:

def call(*popenargs, **kwargs):
    p = subprocess.Popen(*popenargs, **kwargs)
    return p.wait()

关于Python subprocess.call 和 subprocess.Popen 给我不同的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6934696/

相关文章:

python - 如何将 `pip install --user` bin 添加到我的 PATH?

Python Google Cloud Storage 未列出某些文件夹

python - 如何在python子进程中由另一个用户运行文件

Python子进程将变量传递给函数时语法无效

python - 如何将整数值传递给 python 的 subprocess.call 方法?

python - 分别分割每个轮廓

Python 网络应用程序 - 同步串行访问

Python:如何切断字符串中超过2个相等字符的序列

python - 为什么在 Sublime Text 下写入 STD_OUTPUT_HANDLE 会崩溃?

java - Maven 命令 mvn 从终端运行没有错误,但从 python 没有错误