Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python subprocess.Popen进程stdout是否返回空?_Python_Python 3.x_Subprocess - Fatal编程技术网

Python subprocess.Popen进程stdout是否返回空?

Python subprocess.Popen进程stdout是否返回空?,python,python-3.x,subprocess,Python,Python 3.x,Subprocess,我有这个python代码 input() print('spam') 另存为ex1.py 在交互式shell中 >>>from subprocess import Popen ,PIPE >>>a=Popen(['python.exe','ex1.py'],stdout=PIPE,stdin=PIPE) >>> a.communicate() (b'', None) >>> 为什么它不打印垃圾邮件?您要查找的是子流

我有这个python代码

input()
print('spam')
另存为
ex1.py

在交互式shell中

>>>from subprocess import Popen ,PIPE
>>>a=Popen(['python.exe','ex1.py'],stdout=PIPE,stdin=PIPE)

>>> a.communicate()

(b'', None)

>>>

为什么它不打印垃圾邮件?您要查找的是子流程。检查输出需要整行,但您的输入是空的。因此,只有一个异常写入到
stderr
,而
stdout
没有任何异常。至少提供一个换行符作为输入:

>>> a = Popen(['python3', 'ex1.py'], stdout=PIPE, stdin=PIPE)
>>> a.communicate(b'\n')
(b'spam\n', None)
>>> 

您缺少
stderr
管道:

from subprocess import Popen ,PIPE

proc = Popen(['python.exe','ex1.py'], stdout=PIPE, stderr=PIPE)
out, err = proc.communicate()
print(out, err)

可能与@Gator\u Python重复不,这些示例没有任何输入。谢谢,我只是添加了
stderr=PIPE
,然后运行程序,它捕捉到错误为
(b'',b'回溯(最近一次调用):\r\n文件“receive\u from_sub.py”,第5行,在\r\n input()\r\n nEOFError:EOF读取一行时\r\n')
你能再解释一下你的答案是如何解决上述问题的吗?嗨,拉凯什,你看到我在评论中链接的问题了吗?此函数专门用于获取另一个进程的输出。虽然您的答案可能是正确的,但最好解释为什么它是正确的。这将教育OP,帮助他们了解如何避免未来的问题。