在 Python 3.5.1 上,我有以下内容:
output = subprocess.check_output(cmd).decode(encoding="UTF-8")
这会调用正确调用的命令 cmd
。 cmd
中的 C++14 代码如下所示:
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
assert(handle!=INVALID_HANDLE_VALUE); //Always passes
assert(handle!=nullptr); //Always passes
CONSOLE_SCREEN_BUFFER_INFO csbi;
BOOL result = GetConsoleScreenBufferInfo(handle,&csbi);
assert(result!=0); //Always fails. `GetLastError()` returns 6 (invalid handle)
运行上述 Python 代码会导致子进程 cmd
在指示的行失败。根据 Python 文档,在这种情况下,stdout/stderr 应该从父进程(即 Python 解释器)继承。所以,不应该。事实上,上面的例子很好用。 printf
ed 输出。
尝试显式重定向也失败了:
#Traceback (most recent call last):
# File "C:\dev\Python35\lib\subprocess.py", line 914, in __init__
# errread, errwrite) = self._get_handles(stdin, stdout, stderr)
# File "C:\dev\Python35\lib\subprocess.py", line 1145, in _get_handles
# c2pwrite = msvcrt.get_osfhandle(stdout.fileno())
#io.UnsupportedOperation: fileno
p = subprocess.Popen(cmd.split(" "),stdout=sys.stdout,stderr=sys.stderr)
#Using `subprocess.PIPE` instead fails in the same way as the `subprocess.check_output(...)`
#example originally given above.
出了什么问题?我该如何解决?
最佳答案
管道不是控制台。 check_output()
在内部使用 stdout=PIPE
。它不会重定向控制台输出(由 WriteConsoleW()
生成)。
第一个错误(无效句柄)表明标准输出不是控制台设备。
除非这是期望的行为(to print outside of the standard output);使用 WriteFile()
如果 stdout is not a console device .
第二个错误 (io.UnsupportedOperation: fileno
) 表明 sys.stdout
不是真实文件,因此您不能将其作为 stdout 传递
子进程的参数(您可以 redirect subprocess' stdout using stdout=PIPE
and print the output using print()
or sys.stdout.write()
method directly )。
关于python - 将 Python 标准 IO 暴露给子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37623664/