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实现聊天机器人的示例代码

一、AIML是什么 AIML全名为Artificial Intelligence Markup Language(人工智能标记语言),是一种创建自然语言软件代理的XML语言,是由Ric...

Python发送邮件测试报告操作实例详解

Python发送邮件测试报告操作实例详解

本文实例讲述了Python发送邮件测试报告操作。分享给大家供大家参考,具体如下: 发邮件需要用到python两个模块,smtplib和email,这俩模块是python自带的,只需imp...

浅谈pytorch和Numpy的区别以及相互转换方法

如下所示: # -*- coding: utf-8 -*- # @Time : 2018/1/17 16:37 # @Author : Zhiwei Zhong # @Site...

python opencv判断图像是否为空的实例

如下所示: import cv2 im = cv2.imread('2.jpg') if im is None: print("图像为空") # cv2.imshow("ss...

Python ftp上传文件

以下代码比较简单,对python实现ftp上传文件相关知识感兴趣的朋友可以参考下 #encoding=utf8 from ftplib import FTP #加载ftp模块 IP...