最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
python 执行shell命令并将结果保存的实例
时间:2018-05-17 编辑:猪哥 来源:一聚教程网
方法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堆排序原理与实现方法详解
相关文章
- Golang ProtoBuf的基本语法详解 10-20
- Python识别MySQL中的冗余索引解析 10-20
- Python+Pygame绘制小球代码展示 10-18
- Python中的数据精度问题介绍 10-18
- Python随机值生成的常用方法介绍 10-18
- python3解压缩.gz文件分析 09-27