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优化技巧之利用ctypes提高执行速度

首先给大家分享一个个人在使用python的ctypes调用c库的时候遇到的一个小坑 这次出问题的地方是一个C函数,返回值是malloc生成的字符串地址。平常使用也没问题,也用了有段时间,...

pyqt5与matplotlib的完美结合实例

具体用到了matplotlib.backends.backend_qt5agg.FigureCanvasQTAgg 直接上代码(这里给出的只是一个简单的框架,告诉你怎么去写): #...

Python+matplotlib实现计算两个信号的交叉谱密度实例

Python+matplotlib实现计算两个信号的交叉谱密度实例

 计算两个信号的交叉谱密度 结果展示: 完整代码: import numpy as np import matplotlib.pyplot as plt fig,...

python3.4控制用户输入与输出的方法

一、输入 1.函数格式:input() 2.函数功能:接受一个标准输入数据,返回string类型。ctrl+z结束输入。 3.实例: 默认input():等待一个任意字符的输入 s...

python入门教程之识别验证码

python入门教程之识别验证码

前言 验证码?我也能破解? 关于验证码的介绍就不多说了,各种各样的验证码在人们生活中时不时就会冒出来,身为学生日常接触最多的就是教务处系统的验证码了,比如如下的验证码: 识别办法...