Python調用shell cmd方法代碼示例解析
1.使用os.system()去調用,但是只能返回執行狀態,不能獲取shell cmd執行結果
#!/usr/bin/python# -*- coding: utf-8import osstatus = os.system('ps aux |grep Xcode |grep -v grep')print status
2.使用os.popen執行并獲取結果
如果返回是str,直接通過read拿結果使用,如果是多行,選擇readlines轉list獲取每行內容
#整份字符串處理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,直接拿結果使用,如果是多行,選擇用splitline轉list獲取
import commandsstatus, output = commands.getstatusoutput(’ps aux |grep Xcode |grep -v grep’)print outputoutput_list = output.splitlines()print output_list
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
1. 解決Android studio xml界面無法預覽問題2. 基于android studio的layout的xml文件的創建方式3. 詳解Android studio 動態fragment的用法4. Android如何加載Base64編碼格式圖片5. 圖文詳解vue中proto文件的函數調用6. Spring Boot和Thymeleaf整合結合JPA實現分頁效果(實例代碼)7. 什么是python的自省8. .Net Core使用Coravel實現任務調度的完整步驟9. Vuex localStorage的具體使用10. Vue封裝一個TodoList的案例與瀏覽器本地緩存的應用實現

網公網安備