举例讲解Python程序与系统shell交互的方式


Posted in Python onApril 09, 2015

概述

考虑这样一个问题,有hello.py脚本,输出”hello, world!”;有TestInput.py脚本,等待用户输入,然后打印用户输入的数据。那么,怎么样把hello.py输出内容发送给TestInput.py,最后TestInput.py打印接收到的”hello, world!”。下面我来逐步讲解一下shell的交互方式。

hello.py代码如下:

#!/usr/bin/python
print "hello, world!"

TestInput.py代码如下:

#!/usr/bin/python
str = raw_input()
print("input string is: %s" % str)

1.os.system(cmd)

这种方式只是执行shell命令,返回一个返回码(0表示执行成功,否则表示失败)

retcode = os.system("python hello.py")
print("retcode is: %s" % retcode);

输出:

hello, world!
retcode is: 0

2.os.popen(cmd)

执行命令并返回该执行命令程序的输入流或输出流.该命令只能操作单向流,与shell命令单向交互,不能双向交互.
返回程序输出流,用fouput变量连接到输出流

fouput = os.popen("python hello.py")
result = fouput.readlines()
print("result is: %s" % result);

输出:

result is: ['hello, world!\n']

返回输入流,用finput变量连接到输出流

finput = os.popen("python TestInput.py", "w")
finput.write("how are you\n")

输出:

input string is: how are you

3.利用subprocess模块

subprocess.call()

类似os.system(),注意这里的”shell=True”表示用shell执行命令,而不是用默认的os.execvp()执行.

f = call("python hello.py", shell=True)
print f

输出:

hello, world!
0

subprocess.Popen()

利用Popen可以是实现双向流的通信,可以将一个程序的输出流发送到另外一个程序的输入流.
Popen()是Popen类的构造函数,communicate()返回元组(stdoutdata, stderrdata).

p1 = Popen("python hello.py", stdin = None, stdout = PIPE, shell=True)
p2 = Popen("python TestInput.py", stdin = p1.stdout, stdout = PIPE, shell=True)
print p2.communicate()[0]
#other way
#print p2.stdout.readlines()

输出:

input string is: hello, world!

整合代码如下:

#!/usr/bin/python
import os
from subprocess import Popen, PIPE, call

retcode = os.system("python hello.py")
print("retcode is: %s" % retcode);

fouput = os.popen("python hello.py")
result = fouput.readlines()
print("result is: %s" % result);

finput = os.popen("python TestInput.py", "w")
finput.write("how are you\n")


f = call("python hello.py", shell=True)
print f

p1 = Popen("python hello.py", stdin = None, stdout = PIPE, shell=True)

p2 = Popen("python TestInput.py", stdin = p1.stdout, stdout = PIPE, shell=True)
print p2.communicate()[0]
#other way
#print p2.stdout.readlines()
Python 相关文章推荐
python获取外网ip地址的方法总结
Jul 02 Python
Python实现线程状态监测简单示例
Mar 28 Python
Python 实现使用dict 创建二维数据、DataFrame
Apr 13 Python
Django 使用Ajax进行前后台交互的示例讲解
May 28 Python
selenium+python实现自动化登录的方法
Sep 04 Python
对python中Json与object转化的方法详解
Dec 31 Python
python scatter散点图用循环分类法加图例
Mar 19 Python
Python面向对象程序设计类变量与成员变量、类方法与成员方法用法分析
Apr 12 Python
Python正则表达式匹配数字和小数的方法
Jul 03 Python
Python装饰器使用你可能不知道的几种姿势
Oct 25 Python
PyCharm 2020.2.2 x64 下载并安装的详细教程
Oct 15 Python
python本地文件服务器实例教程
May 02 Python
使用Python中的cookielib模拟登录网站
Apr 09 #Python
列举Python中吸引人的一些特性
Apr 09 #Python
Python的Bottle框架的一些使用技巧介绍
Apr 08 #Python
在Python的框架中为MySQL实现restful接口的教程
Apr 08 #Python
简单介绍Python的轻便web框架Bottle
Apr 08 #Python
常见的在Python中实现单例模式的三种方法
Apr 08 #Python
分析Python的Django框架的运行方式及处理流程
Apr 08 #Python
You might like
php 动态多文件上传
2009/01/18 PHP
php 表单数据的获取代码
2009/03/10 PHP
php 魔术函数使用说明
2010/02/21 PHP
ECMall支持SSL连接邮件服务器的配置方法详解
2014/05/19 PHP
实现PHP+Mysql无限分类的方法汇总
2015/03/02 PHP
php开发微信支付获取用户地址
2015/10/04 PHP
CakePHP框架Model函数定义方法示例
2017/08/04 PHP
ThinkPHP框架整合微信支付之JSAPI模式图文详解
2019/04/09 PHP
YII2框架中ActiveDataProvider与GridView的配合使用操作示例
2020/03/18 PHP
Iframe thickbox2.0使用的方法
2009/03/05 Javascript
struts2 jquery 打造无限层次的树
2009/10/23 Javascript
用Juery网页选项卡实现代码
2011/06/13 Javascript
js setTimeout 参数传递使用介绍
2013/08/13 Javascript
JavaScript使用function定义对象并调用的方法
2015/03/23 Javascript
使用Bootstrap + Vue.js实现添加删除数据示例
2017/02/27 Javascript
使用ionic播放轮询广告的实现方法(必看)
2017/04/24 Javascript
JavaScript运动框架 链式运动到完美运动(五)
2017/05/18 Javascript
babel的使用及安装配置教程
2018/02/22 Javascript
谈谈为什么你的 JavaScript 代码如此冗长
2019/01/30 Javascript
vue实现设置载入动画和初始化页面动画效果
2019/10/28 Javascript
Python 不同对象比较大小示例探讨
2014/08/21 Python
用Python的SimPy库简化复杂的编程模型的介绍
2015/04/13 Python
Python的标准模块包json详解
2017/03/13 Python
python Flask 装饰器顺序问题解决
2018/08/08 Python
基于python生成器封装的协程类
2019/03/20 Python
Python3.0中普通方法、类方法和静态方法的比较
2019/05/03 Python
Python中and和or如何使用
2020/05/28 Python
python爬取”顶点小说网“《纯阳剑尊》的示例代码
2020/10/16 Python
python实现启动一个外部程序,并且不阻塞当前进程
2020/12/05 Python
万年牢教学反思
2014/02/15 职场文书
支部鉴定材料
2014/06/02 职场文书
2014三年级班主任工作总结
2014/12/05 职场文书
大学生自我评价范文
2015/03/03 职场文书
那些美到让人窒息的诗句,值得你收藏!
2019/08/20 职场文书
七年级作文之雪景
2019/11/18 职场文书
MySQL删除和插入数据很慢的问题解决
2021/06/03 MySQL