Python与shell的3种交互方式介绍


Posted in Python onApril 11, 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!

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之折腾一下目录
Oct 24 Python
详解Python map函数及Python map()函数的用法
Nov 16 Python
Python3实现的Mysql数据库操作封装类
Jun 06 Python
python 定义给定初值或长度的list方法
Jun 23 Python
python requests 测试代理ip是否生效
Jul 25 Python
win10系统下Anaconda3安装配置方法图文教程
Sep 19 Python
selenium在执行phantomjs的API并获取执行结果的方法
Dec 17 Python
python采集微信公众号文章
Dec 20 Python
python 实现二维列表转置
Dec 02 Python
tensorflow 报错unitialized value的解决方法
Feb 06 Python
windows下的pycharm安装及其设置中文菜单
Apr 23 Python
python实现批量移动文件
Apr 05 Python
Python函数参数类型*、**的区别
Apr 11 #Python
Python中的多重装饰器
Apr 11 #Python
Python中的各种装饰器详解
Apr 11 #Python
将Django使用的数据库从MySQL迁移到PostgreSQL的教程
Apr 11 #Python
Python返回真假值(True or False)小技巧
Apr 10 #Python
Python选择排序、冒泡排序、合并排序代码实例
Apr 10 #Python
Python字符串中查找子串小技巧
Apr 10 #Python
You might like
destoon找回管理员密码的方法
2014/06/21 PHP
php动态函数调用方法
2015/05/21 PHP
PHP实现读取文件夹及批量重命名文件操作示例
2019/04/15 PHP
不用写JS也能使用EXTJS视频演示
2008/12/29 Javascript
javascript中xml操作实现代码
2011/11/21 Javascript
js倒计时小程序
2013/11/05 Javascript
JQuery操作iframe父页面与子页面的元素与方法(实例讲解)
2013/11/20 Javascript
js data日期初始化的5种方法
2013/12/29 Javascript
js与css实现弹出层覆盖整个页面的方法
2014/12/13 Javascript
jquery使用正则表达式验证email地址的方法
2015/01/22 Javascript
jQuery中extend函数详解
2015/02/13 Javascript
原生js和jquery实现图片轮播特效
2015/04/23 Javascript
使用CoffeeScrip优美方式编写javascript代码
2015/10/28 Javascript
在web中js实现类似excel的表格控件
2016/09/01 Javascript
JavaScript学习笔记整理_用于模式匹配的String方法
2016/09/19 Javascript
使用JavaScript获取Request中参数的值方法
2016/09/27 Javascript
微信小程序 网络请求(post请求,get请求)
2017/01/17 Javascript
原生js实现节日时间倒计时功能
2017/01/18 Javascript
使用jQuery操作DOM的方法小结
2017/02/27 Javascript
使用JS实现图片轮播的实例(前后首尾相接)
2017/09/21 Javascript
Vue创建头部组件示例代码详解
2018/10/23 Javascript
微信小程序学习笔记之文件上传、下载操作图文详解
2019/03/29 Javascript
js的Object.assign用法示例分析
2020/03/05 Javascript
JavaScript事件委托实现原理及优点进行
2020/08/29 Javascript
Python内置模块turtle绘图详解
2017/12/09 Python
对python程序内存泄漏调试的记录
2018/06/11 Python
在django admin中添加自定义视图的例子
2019/07/26 Python
浅谈tensorflow中Dataset图片的批量读取及维度的操作详解
2020/01/20 Python
Matplotlib使用字符串代替变量绘制散点图的方法
2020/02/17 Python
Python如何实现小程序 无限求和平均
2020/02/18 Python
详解Anaconda安装tensorflow报错问题解决方法
2020/11/01 Python
Desigual德国官网:在线购买原创服装
2018/03/27 全球购物
anello泰国官方网站:日本流行包包品牌
2019/08/08 全球购物
小学生作文评语集锦
2014/12/25 职场文书
幼师自荐信范文
2015/03/06 职场文书
本地通过nginx配置反向代理的全过程记录
2021/03/31 Servers