一、
os.system()
調用系統命令,完成后退出,返回值是腳本的退出狀態碼,只會有0(成功),-1(失敗)
沒有返回值,執行多條命令需寫在一個方法里 os.system('cd /usr/local && mkdir aaa.txt')
由於使用該函數經常會莫名其妙地出現錯誤,但是直接執行命令並沒有問題,所以一般建議不要使用。
二、
os.popen()
可以理解為一個管道,返回值可以被python繼續使用,返回的是一個file read對象
output = os.popen('cat /usr/local/aaa.txt')
print output.read()
三、
commands.getstatusoutput()
可以獲得返回值和輸出
status,output = commands.getstatusoutput('cat /proc/cpuinfo')
四、
subprocess.call("abd devices")
subprocess.Popen(cmd, shell=True,stdout = open("a.txt","w")).stdout
