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