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

相关文章

Django中的CACHE_BACKEND参数和站点级Cache设置

CACHE_BACKEND参数 每个缓存后端都可能使用参数。 它们在CACHE_BACKEND设置中以查询字符串形式给出。 有效参数如下:     t...

Python中read()、readline()和readlines()三者间的区别和用法

前言 众所周知在python中读取文件常用的三种方法:read(),readline(),readlines(),今天看项目是又忘记他们的区别了。以前看书的时候觉得这东西很简单,一眼扫过...

python中Switch/Case实现的示例代码

学习Python过程中,发现没有switch-case,过去写C习惯用Switch/Case语句,官方文档说通过if-elif实现。所以不妨自己来实现Switch/Case功能。 使用...

解决python flask中config配置管理的问题

在项目中我们需要配置各种环境。如果我们的配置项很少的话,可以直接简单粗暴的来; 比如: app =Flask(__name__) app.config['DEBUG']=True...

Python学习笔记之open()函数打开文件路径报错问题

Python学习笔记之open()函数打开文件路径报错问题

要以读文件的模式打开一个文件对象,使用Python内置的open()函数,传入文件名和标示符,标示符'r'表示读。 >>> f = open('D:/test.tx...