python 捕获 shell/bash 脚本的输出结果实例

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

相关文章

pytorch对可变长度序列的处理方法详解

pytorch对可变长度序列的处理方法详解

主要是用函数torch.nn.utils.rnn.PackedSequence()和torch.nn.utils.rnn.pack_padded_sequence()以及torch.nn...

利用Python开发实现简单的记事本

利用Python开发实现简单的记事本

前言 本文的操作环境:ubuntu,Python2.7,采用的是Pycharm进行代码编辑,个人很喜欢它的代码自动补齐功能。 示例图 如上图,我们可以看到这个记事本主要分为三个模块:文...

对python numpy数组中冒号的使用方法详解

对python numpy数组中冒号的使用方法详解

python中冒号实际上有两个意思:1.默认全部选择;2. 指定范围。 下面看例子 定义数组 X=array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[1...

Python 2与Python 3版本和编码的对比

一、版本对比 首先要说的是,Python的版本,目前主要分为两大类: Python 2.x的版本的,被称为Python2:是目前用的最广泛的,比如Python 2.7.3。 Python...

Python实现遍历目录的方法【测试可用】

Python实现遍历目录的方法【测试可用】

本文实例讲述了Python实现遍历目录的方法。分享给大家供大家参考,具体如下: # *-* coding=gb2312 *-* import os.path import shuti...