python运行命令_Python中执行外部命令
有很多需求需要在Python中執行shell命令、啟動子進程,并捕獲命令的輸出和退出狀態碼,類似于Java中的Runtime類庫。
subprocess模塊的使用:
Python使用最廣泛的是標準庫的subprocess模塊,用來替換os.system(),os.spawn*(),os.popen*()和commands.*等模塊與函數。
使用subprocess最簡單的方式就是用它提供的便利函數,call,check_all與check_output,當便利函數滿足不了要求再使用Popen類。
1. call
subprocess(args,*,stdin=None,stout=None,stderr=None,shell=False)
shell=True,Python會先運行一個shell,再用shell解釋字符串,而不是傳遞一個列表。
2. check_call
check_all與call類似,只是遇到異常情況返回的形式不同,它會拋出subprocess.CalledProcessError異常
3. check_output
這個便利函數是使用最多了,它可以獲取命令的結果,而不是退出狀態碼
如果想要捕捉退出狀態碼,可以通過拋出的subprocess.CalledProcessError異常
import subprocess
try:
output = subprocess.check_output('ls /zz',shell=True)
except subprocess.CalledProcessError as e:
output = e.output
code = e.returncode
print (code,output)
執行結果:
如果想捕獲命令的錯誤輸出,需將錯誤輸出重定向到標準輸出
subprocess.check_output(['cmd','arg1','arg2'],stderr=subprocess.STDOUT)
4. 使用Popen
下面的exexute_cmd函數對Popen,進行了封裝,執行成功,返回標準輸出和狀態碼,失敗是返回狀態碼和錯誤輸出。
#!/usr/bin/python
#coding=utf8
import subprocess
def execute_cmd(cmd):
p = subprocess.Popen(cmd,
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
if p.returncode != 0:
return p.returncode, stderr
return p.returncode, stdout
if __name__=='__main__':
cmd='ls /u01'
returncode,out=execute_cmd(cmd)
if returncode != 0:
raise SystemExit('execute {0} err :{1}'.format(cmd,out))
else:
print("execute command ({0} sucessful)".format(cmd))
目錄不存時:
總結
以上是生活随笔為你收集整理的python运行命令_Python中执行外部命令的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle rac实例切换,RAC+单
- 下一篇: mysql如何给表字段加密_Mysql