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进行数据提取的方法总结

准备工作 首先是准备工作,导入需要使用的库,读取并创建数据表取名为loandata。 import numpy as np import pandas as pd loandata...

python2与python3共存问题的解决方法

python现在主要使用的有2个版本:2.x和3.x,而这2个版本的语法却有很多的不同,python3.x并不是向下兼容2.x的。虽然说3.x是未来python的主流,但是很多工具和个人...

Django Form 实时从数据库中获取数据的操作方法

Django Form 实时从数据库中获取数据的操作方法

Django Form 实时从数据库中获取数据 ,具体内容如下所示: 修改 models.py 添加 class UserType(models.Model): caption =...

Python判断变量是否为Json格式的字符串示例

Json介绍 全名JavaScript Object Notation,是一种轻量级的数据交换格式。Json最广泛的应用是作为AJAX中web服务器和客户端的通讯的数据格式。现在也常用于...

提升Python程序性能的7个习惯

掌握一些技巧,可尽量提高Python程序性能,也可以避免不必要的资源浪费。 1、使用局部变量 尽量使用局部变量代替全局变量:便于维护,提高性能并节省内存。 使用局部变量替换模块名字空间中...