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

相关文章

python3 实现一行输入,空格隔开的示例

相信很多人都会使用 a=input() b=input() 来进行输入操作, 但是,这是以换行进行操作的,而有些题目是需要进行一行输入,空格隔开的,这时候就需要变换输入方式了。 那...

详解django的serializer序列化model几种方法

序列化是将对象状态转换为可保持或传输的格式的过程。与序列化相对的是反序列化,它将流转换为对象。这两个过程结合起来,可以轻松地存储和传输数据 方法一:通过继承View类,重写get方法,获...

Python实现高效求解素数代码实例

素数是编程中经常需要用到的。 作为学习Python的示例,下面是一个高效求解一个范围内的素数的程序,不需要使用除法或者求模运算。 #coding:utf-8 #设置python...

解决Django中修改js css文件但浏览器无法及时与之改变的问题

解决Django中修改js css文件但浏览器无法及时与之改变的问题

今天修改之前实习小伙伴写的js代码的时候,遇到修改后页面未发生变化的问题。因为我是web开发小白,所以上网查了一波,得以解决~~ 初次进行web工程开发的人可能会碰到这样的情况:自己在明...

Python实现从订阅源下载图片的方法

本文实例讲述了Python实现从订阅源下载图片的方法。分享给大家供大家参考。具体如下: 这段代码是基于python 3.4实现的,和python2.X 比起来有了好多差别啊。 这是一个练...