dev

[python] shell command 실행하기

temp_information 2014. 10. 15. 14:27

python 코드에서 shell script 실행하는 가장 간단한 방법

>>> import os

>>> os.system("command to execute")


example

>>> import os

>>> os.system("ls -a")


좀더 복잡한 방법

using subprocess

ref this link)

http://stackoverflow.com/questions/4760215/running-shell-command-from-python-and-capturing-the-output


example

>> import subprocess

>> commandStr = "nvidia-smi"

>> result = subprocess.check_output(commandStr, shell=True)