Python調(diào)用shell cmd方法代碼示例解析
1.使用os.system()去調(diào)用,但是只能返回執(zhí)行狀態(tài),不能獲取shell cmd執(zhí)行結(jié)果
#!/usr/bin/python# -*- coding: utf-8import osstatus = os.system('ps aux |grep Xcode |grep -v grep')print status
2.使用os.popen執(zhí)行并獲取結(jié)果
如果返回是str,直接通過read拿結(jié)果使用,如果是多行,選擇readlines轉(zhuǎn)list獲取每行內(nèi)容
#整份字符串處理p=os.popen(’ps aux |grep Xcode |grep -v grep’) res=p.read()print res,type(res)p.close()#多行處理p=os.popen(’ps aux |grep Xcode |grep -v grep’) res1=p.readlines()for line in res1: print ’line :’+linep.close()
3.使用commands 模塊commands.getstatusoutput()
如果返回是str,直接拿結(jié)果使用,如果是多行,選擇用splitline轉(zhuǎn)list獲取
import commandsstatus, output = commands.getstatusoutput(’ps aux |grep Xcode |grep -v grep’)print outputoutput_list = output.splitlines()print output_list
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Python2.6版本pip安裝步驟解析2. python公司內(nèi)項(xiàng)目對(duì)接釘釘審批流程的實(shí)現(xiàn)3. python中Ansible模塊的Playbook的具體使用4. Python自動(dòng)化之定位方法大殺器xpath5. Python本地及虛擬解釋器配置過程解析6. Python 利用flask搭建一個(gè)共享服務(wù)器的步驟7. 基于python實(shí)現(xiàn)matlab filter函數(shù)過程詳解8. Python中Anaconda3 安裝gdal庫的方法9. python自動(dòng)化測(cè)試三部曲之request+django實(shí)現(xiàn)接口測(cè)試10. Python importlib模塊重載使用方法詳解
