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

yipeiwu_com6年前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设计】~

相关文章

python实现xlsx文件分析详解

python实现xlsx文件分析详解

python脚本实现xlsx文件解析,供大家参考,具体内容如下 环境配置: 1.系统环境:Windows 7 64bit 2.编译环境:Python3.4.3 3.依赖库: os s...

使用python绘制温度变化雷达图

使用python绘制温度变化雷达图

本文实例为大家分享了python绘制温度变化雷达图的具体代码,供大家参考,具体内容如下 假设某天某地每三个小时取样的气温为 针对温度变化趋势绘制雷达图: 代码如下: import...

Python中修改字符串的四种方法

在Python中,字符串是不可变类型,即无法直接修改字符串的某一位字符。  因此改变一个字符串的元素需要新建一个新的字符串。 常见的修改方法有以下4种。 方法1:将字符串转...

PyQt5每天必学之带有标签的复选框

PyQt5每天必学之带有标签的复选框

QCheckBox 是具有两种状态的控件:开和关。它是一个带有标签的复选框。复选框通常用于表示应用程序可以启用或禁用的功能。 #!/usr/bin/python3 # -*- cod...

Python实现二叉树的常见遍历操作总结【7种方法】

本文实例讲述了Python实现二叉树的常见遍历操作。分享给大家供大家参考,具体如下: 二叉树的定义: class TreeNode: def __init__(self, x):...