Python:subprocess
Use the os.system
이 방법은 반환값으로 프로그램 실행 결과코드1가 반환된다.
Use the os.popen
os.popen 시리즈는 python 2.0에 생겨서 2.6이 되면서 subprocess의 popen으로 바꿔 쓰기를 권장한다. subprocess는 2.4 버전에 생겼다.
import os
def execute(cmd):
std_in, std_out, std_err = os.popen3(cmd)
return std_out, std_err
cmd = "ls -l"
std_out, std_err = execute(cmd)
for line in std_out.readlines():
print line
Use the subprocess
import subprocess
def execute(cmd) :
fd = subprocess.Popen(cmd, shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
return fd.stdout, fd.stderr
cmd = "ls -l"
std_out, std_err = execute(cmd)
for line in std_out.readlines():
print line
See also
- Python
- Python:asyncio
- Asyncio:AsyncPythonSubprocess - Python의 asyncio패키지를 사용하여 Subprocess를 기동하는 클래스 구현.
Favorite site
- 17. Interprocess Communication and Networking » 17.1. subprocess — Subprocess management
- 파이썬 - 외부 프로그램 실행
- Python command 명령, os 명령 사용방법
- [추천] os.popen3 와 subprocess.Popen 사용
- 파이썬에서 bash 명령어 실행 subprocess, pexpect
- subprocess (Python) 파이썬에서 응용프로그램 실행하기
- 파이썬 외부 실행 결과 저장하기