输出不到标准输出

2024-10-04 01:35:32 发布

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

我有一些可执行文件,这是一个命令处理器。我试图捕捉它对命令的反应。我使用python子进程。你知道吗

下面是我的剧本:

import subprocess

# Open the subprocess
proc = subprocess.Popen('comproc.exe', \
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr = subprocess.STDOUT)

# Write a command
proc.stdin.write('help cfg_open\n')

# Close the input stream so the subprocess knows to exit
proc.stdin.close()

data = 0
while data!="":
    data = proc.stdout.readline()
    print data

# Wait for subprocess to exit
exit_status = proc.wait()

在我发出“help cfg\u open”之前的输出(“Welcome to command proc(c)2015…”)被捕获,而对“help cfg\u open”的响应则没有。 如果我发出一些不存在的命令,stderr就会被正确捕获。你知道吗

通过cmd重定向的效果非常好:

c:\>comproc.exe >1.txt
help cfg_open
exit
c:\>

我在1.txt中得到整个输出。你知道吗

如有任何帮助,我将不胜感激!你知道吗


Tags: theto命令datastdinstdoutexithelp