【问题标题】:Python subprocess.call and subprocess.Popen giving me different outputsPython subprocess.call 和 subprocess.Popen 给我不同的输出
【发布时间】:2011-10-19 13:35:57
【问题描述】:

使用 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

【问题讨论】:

    标签: python subprocess


    【解决方案1】:

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

    subprocess.call(*popenargs, **kwargs) 运行带参数的命令。等待命令完成,然后返回 returncode 属性。

    所以subprocess.call基本上相当于下面的代码,只是为了方便而存在:

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

    【讨论】:

    • 我找到了! process.wait() 会给我相同的返回码。 :)
    猜你喜欢
    • 2010-12-13
    • 1970-01-01
    • 2018-01-14
    • 1970-01-01
    • 2011-11-26
    • 2012-05-02
    • 1970-01-01
    • 2016-11-30
    • 2015-05-20
    相关资源
    最近更新 更多