python 执行shell命令并将结果保存的实例
2018-05-11 10:38
1061 查看
方法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命令并将结果保存的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- python 执行shell命令并将结果保存
- 在python中执行shell命令,并以字符串形式返回命令执行结果
- Python中执行shell命令的实例
- python SSH模块登录,远程机执行shell命令实例解析
- python输出shell命令执行结果
- windows linux 使用python执行系统命令并将结果保存到变量
- Python实现ssh登录执行shell命令并将结果写入mysql数据库
- 获取python执行shell命令的结果
- linux使用Python上秘钥远程登录ssh执行shell命令
- 用python SSH模块登录,并在远程机执行shell命令
- 执行shell命令并读取执行结果——popen()的用法
- python 捕获shell脚本的输出结果实例
- python执行shell命令的几种方法
- shell中嵌套执行expect命令实例
- Python 2.x如何设置命令执行的超时时间实例
- Python 执行Shell 外部命令
- paip.执行shell cmd 命令uapi java php python总结
- java调用shell命令并获取执行结果
- python 执行shell命令
- Python脚本中执行shell命令