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\n'

在test.py中,通過subprocess.Popen()調用了命令"./test",也就是調用了上面生成的c++可執行文件,捕獲了標准輸出,最后打印格式是二進制。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM