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获取城市近7天天气预报

基于Python获取城市近7天天气预报

这篇文章主要介绍了基于Python获取城市近7天天气预报,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 首先,我们打开中国天气网,找到...

在Python操作时间和日期之asctime()方法的使用

 asctime()方法将一个元组或struct_time表示的时间返回gmtime()或localtime(),以下列格式的24个字符的字符串:“Tue Feb 17 23:...

python环境路径配置以及命令行运行脚本

python环境路径配置以及命令行运行脚本

本文实例为大家分享了python环境路径设置方法,以及命令行运行python脚本,供大家参考,具体内容如下 找Python安装目录,设置环境路径以及在命令行运行python脚本 第一点:...

Python实现全角半角转换的方法

本文实例讲解了Python实现全角半角转换的方法,相信对于大家的Python学习能够起到一定的参考借鉴价值。如下所示: 一、全角半角转换概述: 全角字符unicode编码从65281~6...

Python+Turtle动态绘制一棵树实例分享

Python+Turtle动态绘制一棵树实例分享

本文实例主要是对turtle的使用,实现Python+turtle动态绘制一棵树的实例,具体代码: # drawtree.py from turtle import Turtle...