python 执行shell命令并将结果保存的实例
方法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
相关推荐
以梦为马不负韶华 2020-10-20
tianhuak 2020-11-24
huha 2020-10-16
lianshaohua 2020-09-23
laisean 2020-11-11
zhangjie 2020-11-11
大牛牛 2020-10-30
firefaith 2020-10-30
liguojia 2020-10-20
wangzhaotongalex 2020-10-20
CARBON 2020-10-20
彼岸随笔 2020-10-20
lianshaohua 2020-10-20
yutou0 2020-10-17
JohnYork 2020-10-16
xiaonamylove 2020-10-16
Julyth 2020-10-16
applecarelte 2020-10-16
ourtimes 2020-10-16