方法1: 將shell執行的結果保存到字符串
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執行的結果寫入到指定文件
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 執行shell命令并將結果保存的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林站長站。
新聞熱點
疑難解答