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实现快速计算词频功能。分享给大家供大家参考,具体如下: 这几天看到一位同事的代码,方法如下: def cut_word(body): temp_dict...

python 检查文件mime类型的方法

magic 模块可以检查文件的mime类型,而不是从后缀名来判断,例如判断文件是不是视频或图片类型如下: #检查文件类型 mime_type = magic.from_file(fu...

ansible动态Inventory主机清单配置遇到的坑

ansible动态Inventory主机清单配置遇到的坑

坑1 : 动态主机清单配置,需要按照ansible的要求的格式返回给ansible命令的 源代码如下: 但是在ansible-playbook中使用动态主机配置文件的时候,发生了错误!!...

Python实现堆排序的方法详解

Python实现堆排序的方法详解

本文实例讲述了Python实现堆排序的方法。分享给大家供大家参考,具体如下: 堆排序作是基本排序方法的一种,类似于合并排序而不像插入排序,它的运行时间为O(nlogn),像插入排序而不像...

在Python中用keys()方法返回字典键的教程

 keys()方法返回在字典中的所有可用的键的列表。 语法 以下是keys()方法的语法: dict.keys() 参数    ...