python 捕获 shell/bash 脚本的输出结果实例

yipeiwu_com5年前Python基础

#!/usr/bin/python
## get subprocess module
import subprocess
 
## call date command ##
p = subprocess.Popen("date", stdout=subprocess.PIPE, shell=True)
 
## Talk with date command i.e. read data from stdout and stderr. Store this info in tuple
## Interact with process: Send data to stdin. Read data from stdout and stderr,
## until end-of-file is reached.Wait for process to terminate. The optional input
## argument should be a string to be sent to the child process, or None,
## if no data should be sent to the child. ##
(output, err) = p.communicate()
 
## Wait for date to terminate. Get return returncode ##
p_status = p.wait()
print "Command output : ", output
print "Command exit status/return code : ", p_status
 
## from: http://www.cyberciti.biz/faq/python-run-external-command-and-get-output/

以上就是小编为大家带来的python 捕获 shell/bash 脚本的输出结果实例全部内容了,希望大家多多支持【听图阁-专注于Python设计】~

相关文章

Django框架中间件(Middleware)用法实例分析

Django框架中间件(Middleware)用法实例分析

本文实例讲述了Django框架中间件(Middleware)用法。分享给大家供大家参考,具体如下: 1、面向切面编程 切点(钩子) 切点允许我们动态的在原有逻辑中插入一部分代码...

Python地图绘制实操详解

Python地图绘制实操详解

网上有很多地图绘制的教程,更多趋向于全国地图或者省级地图,但有时我们需要到县级。闲得慌,今天以贵州省毕节市为例,分享一篇Python县级地图的绘制(遥想当时差点把百度翻了个底朝天),希望...

python中执行shell命令的几个方法小结

最近有个需求就是页面上执行shell命令,第一想到的就是os.system, 复制代码 代码如下: os.system('cat /proc/cpuinfo') 但是发现页面上打印的命...

Python实现字典的key和values的交换

有些时候我们不得已要利用values来反向查询key,有没有简单的方法呢? 下面我给大家列举一些方法,方便大家使用 python3 >>> d1={'a':1,'...

python 求某条线上特定x值或y值的点坐标方法

问题可以转换为:求一条垂直于x轴或平行于y轴的直线与该线的交点 import numpy as np import shapely.geometry as SG #某条线 li...