python 執(zhí)行shell命令并將結果保存的實例
更新時間:2018年05月11日 10:38:37 作者:siqi_fighting
今天小編就為大家分享一篇python 執(zhí)行shell命令并將結果保存的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
方法1: 將shell執(zhí)行的結果保存到字符串
def run_cmd(cmd): result_str='' process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) result_f = process.stdout error_f = process.stderr errors = error_f.read() if errors: pass result_str = result_f.read().strip() if result_f: result_f.close() if error_f: error_f.close() return result_str
方法2:將shell執(zhí)行的結果寫入到指定文件
def run_cmd2file(cmd): fdout = open("file_out.log",'a') fderr = open("file_err.log",'a') p = subprocess.Popen(cmd, stdout=fdout, stderr=fderr, shell=True) if p.poll(): return p.wait() return
以上這篇python 執(zhí)行shell命令并將結果保存的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
解決ImportError: cannot import name ‘Imput
您遇到的ImportError: cannot import name ‘Imputer‘錯誤提示表明您嘗試導入一個名為’Imputer’的模塊或類,但是該模塊或類無法找到,本文小編給大家介紹了如何解決這個問題,需要的朋友可以參考下2023-10-10Visual Studio code 配置Python開發(fā)環(huán)境
這篇文章主要介紹了Visual Studio code 配置Python開發(fā)環(huán)境,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09python高手之路python處理excel文件(方法匯總)
用python來自動生成excel數(shù)據(jù)文件。python處理excel文件主要是第三方模塊庫xlrd、xlwt、xluntils和pyExcelerator,除此之外,python處理excel還可以用win32com和openpyxl模塊2016-01-01Python Web框架Flask中使用新浪SAE云存儲實例
這篇文章主要介紹了Python Web框架Flask中使用新浪SAE云存儲實例,本文是對SAE云存儲的簡單封裝,需要的朋友可以參考下2015-02-02