python subprocess读取终端输出内容

参考链接:https://www.cnblogs.com/songzhenhua/p/9312718.html

                  https://www.cnblogs.com/darkchii/p/9013673.html

官方说明:https://docs.python.org/3.8/library/subprocess.html

C++:

test.cpp

1 #include <iostream>
2   
3 int main(void)
4 {
5     std::cout<<"hello"<<std::endl;
6 }

g++  test.cpp -o test

运行:./test

输出:hello

python:

test.py

1 import subprocess
2 screen_str = subprocess.Popen("./test", stdout=subprocess.PIPE, shell=True)
3 screen_str.wait()
4 print(screen_str.stdout.read())

运行:python3 test.py

输出:b'hello '

在test.py中,通过subprocess.Popen()调用了命令"./test",也就是调用了上面生成的c++可执行文件,捕获了标准输出,最后打印格式是二进制。

原文地址:https://www.cnblogs.com/brian-sun/p/13525200.html