參考鏈接: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\n'
在test.py中,通過subprocess.Popen()調用了命令"./test",也就是調用了上面生成的c++可執行文件,捕獲了標准輸出,最后打印格式是二進制。