简体   繁体   English

子流程的输出未保存

[英]Output from subprocess not saved

When I use stdout in my Popen with stdin my process does not run. 当我在带有stdin的Popen中使用stdout时,我的进程无法运行。

def program():
    p = subprocess.Popen(link, shell=True, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
    out = p.communicate(longquery)
    print out

When I run this my out comes out as (None, None) although I do have an output that shows up on the console. 当我运行此命令时,我的输出为(None,None),尽管我确实在控制台上显示了输出。 Why is this happening and how can I save the output that is coming out? 为什么会发生这种情况,如何保存输出的结果?

You need to pipe the output data per the manual : 您需要根据手册使用输出管道:

"to get anything other than None in the result tuple, you need to give stdout=PIPE and/or stderr=PIPE too." “要在结果元组中获得除None以外的任何内容,您还需要提供stdout = PIPE和/或stderr = PIPE。

def program():
    p = subprocess.Popen(link, shell=True, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
    out = p.communicate(longquery)
    print out

You need to set stdout=PIPE as well. 您还需要设置stdout=PIPE

Change your code to: 将您的代码更改为:

from subprocess import Popen, PIPE, STDOUT


def program():
    p = Popen(link, shell=True, stdin=PIPE, stderr=STDOUT, stdout=PIPE)
    out = p.communicate(longquery)
    print out

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM