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实现石头剪刀布程序

本文实例为大家分享了python实现石头剪刀布的具体代码,供大家参考,具体内容如下 概述: 如果你和我一样是一个有着其他语言基础的编程者,那我想这个小程序对于你来说是小case。由于本人...

Python中的数学运算操作符使用进阶

Python中对象的行为是由它的类型 (Type) 决定的。所谓类型就是支持某些特定的操作。数字对象在任何编程语言中都是基础元素,支持加、减、乘、除等数学操作。 Python的数字对象有...

Python3.7 新特性之dataclass装饰器

Python 3.7中一个令人兴奋的新特性是 data classes 。 数据类通常是一个主要包含数据的类,尽管实际上没有任何限制。 它是使用新的 @dataclass 装饰器创建的,...

python按键按住不放持续响应的实例代码

在学习飞机大战(我也不知道为什么都拿这个练手),飞机左右控制都是按键按一次移动一次,不能按住一个键后持续移动,离开后停止移动。 为了解决这个,查看了参考手册,说让用pygame.key....

Python虚拟环境virtualenv的安装与使用详解

前言 动态语言Ruby、Python都有自己的虚拟环境,虚拟环境是程序执行时的独立执行环境,在同一台服务器中可以创建不同的虚拟环境供不同的系统使用,项目之间的运行环境保持独立性而相互不受...