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

相关文章

python入门前的第一课 python怎样入门

python入门前的第一课 python怎样入门

人工智能时代的到来,很多文章说这么一句:“不会python,就不要说自己是程序员”,这说的有点夸张了,但确实觉得目前python这个语言值得学习,而且会python是高薪程序员的必备技能...

python 统计列表中不同元素的数量方法

刚刚上网搜了一下如何用python统计列表中不同元素的数量,发现很少,找了半天。我自己来写一种方法。 代码如下 list=[1,1,2,2,3] print(list) set1=s...

Python Web开发模板引擎优缺点总结

做 Web 开发少不了要与模板引擎打交道。我陆续也接触了 Python 的不少模板引擎,感觉可以总结一下了。 一、首先按照我的熟悉程度列一下:pyTenjin:我在开发 Doodle 和...

你所不知道的Python奇技淫巧13招【实用】

有时候你会看到很Cool的Python代码,你惊讶于它的简洁,它的优雅,你不由自主地赞叹:竟然还能这样写。其实,这些优雅的代码都要归功于Python的特性,只要你能掌握这些Pythoni...

Python数据结构之单链表详解

本文实例为大家分享了Python数据结构之单链表的具体代码,供大家参考,具体内容如下 # 节点类 class Node(): __slots__=['_item','_next'...