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使用wxPython打开并播放wav文件的方法

本文实例讲述了python使用wxPython打开并播放wav文件的方法。分享给大家供大家参考。具体实现方法如下: ''' wx_lib_filebrowsebutton_sound...

Python3中编码与解码之Unicode与bytes的讲解

今天玩Python爬虫,下载一个网页,然后把所有内容写入一个txt文件中,出现错误; TypeError: write() argument must be str, not byte...

Python实现基于PIL和tesseract的验证码识别功能示例

Python实现基于PIL和tesseract的验证码识别功能示例

本文实例讲述了Python实现基于PIL和tesseract的验证码识别功能。分享给大家供大家参考,具体如下: 之前搞这个搞了一段时间,后面遇到了点小麻烦,导致识别率太低了,最多也就百分...

Python中使用gflags实例及原理解析

这篇文章主要介绍了Python中使用gflags实例及原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 安装命令pip inst...

详解Python最长公共子串和最长公共子序列的实现

详解Python最长公共子串和最长公共子序列的实现

最长公共子串(The Longest Common Substring) LCS问题就是求两个字符串最长公共子串的问题。解法就是用一个矩阵来记录两个字符串中所有位置的两个字符之间的匹配情...