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使用multiprocessing实现一个最简单的分布式作业调度系统

 mutilprocess像线程一样管理进程,这个是mutilprocess的核心,他与threading很是相像,对多核CPU的利用率会比threading好的多。 介绍 P...

详解Python 实现元胞自动机中的生命游戏(Game of life)

详解Python 实现元胞自动机中的生命游戏(Game of life)

简介 细胞自动机(又称元胞自动机),名字虽然很深奥,但是它的行为却是非常美妙的。所有这些怎样实现的呢?我们可以把计算机中的宇宙想象成是一堆方格子构成的封闭空间,尺寸为N的空间就有NN个格...

解决Mac下首次安装pycharm无project interpreter的问题

Pycharm本身并不带编译器,所以第一次用需要自己下载编译器插件。 1、首先去 https://www.python.org/downloads/ 这个网址去下载对应的python版本...

Python查询Mysql时返回字典结构的代码

MySQLdb默认查询结果都是返回tuple,输出时候不是很方便,必须按照0,1这样读取,无意中在网上找到简单的修改方法,就是传递一个cursors.DictCursor就行。 默认程序...

python 经典数字滤波实例

python 经典数字滤波实例

数字滤波分为 IIR 滤波,和FIR 滤波。 FIR 滤波: import scipy.signal as signal import numpy as np import pyla...