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从单元素字典中获取key和value的实例

之前写代码很多时候会遇到这么一种情况:在python的字典中只有一个key/value键值对,想要获取其中的这一个元素还要写个for循环获取。 网上搜了一下,发现还有很多简单的方法: 方...

python能做什么 python的含义

python能做什么?是什么意思? Python是一种跨平台的计算机程序设计语言。是一种面向对象的动态类型语言,最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能...

python matlibplot绘制3D图形

python matlibplot绘制3D图形

本文实例为大家分享了python matlibplot绘制3D图形的具体代码,供大家参考,具体内容如下 1、散点图使用scatter from mpl_toolkits.mplot3...

windows下添加Python环境变量的方法汇总

如果安装的时候没有选择Add Python.exe to Path 导致在命令提示符下执行python 提示 'python' 不是内部或外部命令,也不是可运行的程序或批处理文件。 可...

python复制列表时[:]和[::]之间有什么区别

前言 new = old[:] Python老鸟都知道以上代码是什么意思。它复制列表old到new。它对于新手来说是种困惑而且应该避免使用这种方法。不幸的是[:]标记法被广泛使用,...