Python Popen尝试写入不存在的管道
为什么下面的方法不起作用Python Popen尝试写入不存在的管道,python,subprocess,popen,Python,Subprocess,Popen,为什么下面的方法不起作用 import subprocess process = subprocess.Popen('cmd.exe', shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=None) 我得到的输出如下: The process tried to write to a nonexistent pipe. The process tried to write to a nonexistent p
import subprocess
process = subprocess.Popen('cmd.exe', shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=None)
我得到的输出如下:
The process tried to write to a nonexistent pipe.
The process tried to write to a nonexistent pipe.
The process tried to write to a nonexistent pipe.
The process tried to write to a nonexistent pipe.
The process tried to write to a nonexistent pipe.
在此之后,进程将在没有错误的情况下终止。我运行的是Windows 7。我运行的是Windows 10,但也有同样的问题。如果我将stdout和stderr设置为相同的值,错误就会消失。尝试将它们都设置为subprocess.PIPE,而不仅仅是stdout。尝试:
import subprocess
subprocess.run(["echo", "Hello, world!"], shell=True)
这将使用默认的stdout和stderr,而不是创建管道。这是您的完整程序吗?因为您似乎缺少process.communicate()。process.communicate()和变量的输出是相同的。您好!虽然这段代码可以解决这个问题,但如何以及为什么解决这个问题将真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。