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的编码问题

代码实例讲解python3的编码问题

python3的编码问题。 打开python开发工具IDLE,新建‘codetest.py'文件,并写代码如下: import sys print (sys.getdefaulte...

python将list转为matrix的方法

如下所示: import numpy as np tmp = [[1,2,3],[4,5,6],[7,8,9]]; np.matrix(tmp) 以上这篇python将list转...

Python+OpenCV实现实时眼动追踪的示例代码

Python+OpenCV实现实时眼动追踪的示例代码

使用Python+OpenCV实现实时眼动追踪,不需要高端硬件简单摄像头即可实现,效果图如下所示。   项目演示参见:https://www.bilibili.com/vide...

Python实现网站表单提交和模板

Python实现网站表单提交和模板

如果像前面那么做网站,也太丑陋了。并且功能也不多。 在实际做网站中,现在都要使用一个模板,并且在用户直接看到的页面,用html语言来写页面。 在做网站的行业里面,常常将HTML+CSS+...

python 一篇文章搞懂装饰器所有用法(建议收藏)

python 一篇文章搞懂装饰器所有用法(建议收藏)

01. 装饰器语法糖 如果你接触 Python 有一段时间了的话,想必你对 @ 符号一定不陌生了,没错 @ 符号就是装饰器的语法糖。 它放在一个函数开始定义的地方,它就像一顶帽子一样戴...