Python子进程stderr/stdout字段在创建时为None

2024-06-29 00:59:38 发布

您现在位置:Python中文网/ 问答频道 /正文

当我创建一个文件流为stderr(或stdout)的python子进程时,相应的字段是None

s = subprocess.Popen(['ls','qwer'],
                     stdout=subprocess.PIPE,
                     stdin=subprocess.PIPE,
                     stderr=open("zzzzz",'wb'))
s.stderr is None
==> True

(我希望s.stderr是文件流)。在

  • 这是有记录的/有意的吗?在
  • 理由是什么?在

Tags: 文件none进程isstderrstdinstdoutopen
1条回答
网友
1楼 · 发布于 2024-06-29 00:59:38

是的,这是故意的。当您将stdout/stdin/stderrkwarg提供给^{} constructor时,您告诉它在子进程中将这些管道指向何处:

stdin, stdout and stderr specify the executed program’s standard input, standard output and standard error file handles, respectively.

但是Popen对象are documented as only having a value when you use ^{}上的stdout/stdin/stderr属性:

Popen.stdin

If the stdin argument was PIPE, this attribute is a file object that provides input to the child process. Otherwise, it is None.

Popen.stdout

If the stdout argument was PIPE, this attribute is a file object that provides output from the child process. Otherwise, it is None.

Popen.stderr

If the stderr argument was PIPE, this attribute is a file object that provides error output from the child process. Otherwise, it is None.

至于为什么当你传递一个不是PIPE的句柄时Popen.std*是{},我不确定。也许是因为它被认为是多余的,因为你必须首先向构造函数传递一个打开的句柄?在

相关问题 更多 >