본문 바로가기

Computer/Python

[Python] shell에서 쓰는 명령어 실행하기

C++로 컴파일해서 만든 실행파일을 python에서 쓰고 싶었는데, 방법을 몰라 고민하던 차에 나름의 방법을 알아냈다.

참고사이트1

참고사이트2(여기갓갓)

import os
import sys
os.system('./hello_world')

이러면 hello_world 가 실행됨. (출력물 : hello world!)

여기서 나온 결과값을 저장하려면 subprocess를 쓰면 됨.

import subprocess
result = subprocess.check_output('./hello_world', shell=True)
print (result)

이러면 result에 hello world!가 잘 저장되어있다.

와 파이썬 진짜 좋다.....ㅎ...