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易忽视知识点小结

这里记录Python中容易被忽视的小问题 一、input(...)和raw_input(...) #简单的差看帮助文档input(...)和raw_input(...)有如下区别...

python中闭包Closure函数作为返回值的方法示例

前言 首先看看闭包的概念:闭包(Closure)是词法闭包(Lexical Closure)的简称,是引用了自由变量的函数。这个被引用的自由变量将和这个函数一同存在,即使已经离开了创造它...

Pycharm学习教程(1) 定制外观

Pycharm学习教程(1) 定制外观

  Pycharm作为一款强力的Python IDE,在使用过程中感觉一直找不到全面完整的参考手册,因此决定对官网的Pycharm教程进行简要翻译,与大家分享。 1、准备工作   首先...

pandas取出重复数据的方法

drop_duplicates为我们提供了数据去重的方法,那怎么得到哪些数据有重复呢? 实现步骤: 1、采用drop_duplicates对数据去两次重,一次将重复数据全部去除(...

基于python中pygame模块的Linux下安装过程(详解)

一、使用pip安装Python包 大多数较新的Python版本都自带pip,因此首先可检查系统是否已经安装了pip。在Python3中,pip有时被称为pip3. 1、在Linux和OS...