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端口扫描简单程序

本文实例为大家分享了Python端口扫描的实现代码,供大家参考,具体内容如下 获取本机的IP和端口号: import socket def get_my_ip(): t...

浅谈python中统计计数的几种方法和Counter详解

1) 使用字典dict() 循环遍历出一个可迭代对象中的元素,如果字典没有该元素,那么就让该元素作为字典的键,并将该键赋值为1,如果存在就将该元素对应的值加1. lists = ['...

Django框架model模型对象验证实现方法分析

本文实例讲述了Django框架model模型对象验证实现方法。分享给大家供大家参考,具体如下: 模型对象的验证 验证一个模型涉及三个步骤: 验证模型的字段 —— Model.cle...

Python实现二叉树的常见遍历操作总结【7种方法】

本文实例讲述了Python实现二叉树的常见遍历操作。分享给大家供大家参考,具体如下: 二叉树的定义: class TreeNode: def __init__(self, x):...

python 中文乱码问题深入分析

python 中文乱码问题深入分析

在本文中,以'哈'来解释作示例解释所有的问题,“哈”的各种编码如下: 1. UNICODE (UTF8-16),C854; 2. UTF-8,E59388; 3. GBK,B9FE。 一...