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中的yield使用方法

今天在看其他同事的代码时,发现一个没使用过的python关键字 :yield       先问了一下同事,听他说了几句,有个模糊的印象,...

python+Django+apache的配置方法详解

本文实例讲述了python+Django+apache的配置方法。分享给大家供大家参考,具体如下: 下载安装xampp套件 下载mod_python-3.3.1.win32-py2.5-...

Python 类与元类的深度挖掘 I【经验】

  上一篇介绍了 Python 枚举类型的标准库,除了考虑到其实用性,还有一个重要的原因是其实现过程是一个非常好的学习、理解 Python 类与元类的例子。因此接下来两篇就以此为例,深入...

Python写的一个定时重跑获取数据库数据

Python写的一个定时重跑获取数据库数据

做大数据的童鞋经常会写定时任务跑数据,由于任务之间的依赖(一般都是下游依赖上游的数据产出),所以经常会导致数据获取失败,因为很多人发现数据失败后 都会去查看日志,然后手动去执行自己的任务...

Python使用pymysql从MySQL数据库中读出数据的方法

python3.x已经不支持mysqldb了,支持的是pymysql 使用pandas读取MySQL数据时,使用sqlalchemy,出现No module named ‘MySQLdb...