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获取从命令行输入数字的方法

本文实例讲述了python获取从命令行输入数字的方法。分享给大家供大家参考。具体如下: #---------------------------------------- #...

python使用clear方法清除字典内全部数据实例

本文实例讲述了python使用clear方法清除字典内全部数据。分享给大家供大家参考。具体实现方法如下: d = {} d['name'] = 'Gumby' d['age'] =...

使用 Python 写一个简易的抽奖程序

使用 Python 写一个简易的抽奖程序

不知道有多少人是被这个头图骗进来的:) 事情的起因是这样的,上周有同学问小编,看着小编的示例代码敲代码,感觉自己也会写了,如果不看的话,七七八八可能也写的出来,但是一旦自己独立写一段程...

python 多进程队列数据处理详解

我就废话不多说了,直接上代码吧! # -*- coding:utf8 -*- import paho.mqtt.client as mqtt from multiprocessing...

Python+numpy实现矩阵的行列扩展方式

Python+numpy实现矩阵的行列扩展方式

对于numpy矩阵,行列扩展有三种比较常用的方法: 1、使用矩阵对象的c_方法扩展列,使用矩阵对象的r_方法扩展行。 2、使用numpy扩展库提供的insert()函数,使用axis参数...