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 相关文章推荐
DJANGO-ALLAUTH社交用户系统的安装配置
Nov 18 Python
讲解Python中的标识运算符
May 14 Python
django实现分页的方法
May 26 Python
使用pdb模块调试Python程序实例
Jun 02 Python
浅谈Python中列表生成式和生成器的区别
Aug 03 Python
利用python为运维人员写一个监控脚本
Mar 25 Python
Python 爬取必应壁纸的实例讲解
Feb 24 Python
python中前缀运算符 *和 **的用法示例详解
May 28 Python
python如何输出反斜杠
Jun 18 Python
python连接mysql数据库并读取数据的实现
Sep 25 Python
Python数据分析库pandas高级接口dt的使用详解
Dec 11 Python
详解python的内存分配机制
May 10 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
无线电的诞生过程
2021/03/01 无线电
推荐几款用 Sublime Text 开发 Laravel 所用到的插件
2014/10/30 PHP
Thinkphp实现自动验证和自动完成
2015/12/19 PHP
php+redis实现注册、删除、编辑、分页、登录、关注等功能示例
2017/02/15 PHP
thinkPHP实现的省市区三级联动功能示例
2017/05/05 PHP
如何让页面在打开时自动刷新一次让图片全部显示
2012/12/17 Javascript
NodeJS Express框架中处理404页面一个方式
2014/05/28 NodeJs
jquery实现先淡出再折叠收起的动画效果
2015/08/07 Javascript
Node.js的Web模板引擎ejs的入门使用教程
2016/06/06 Javascript
javascript实现随机生成DIV背景色
2016/06/20 Javascript
如何手动实现es5中的bind方法详解
2018/12/07 Javascript
vue接入腾讯防水墙代码
2019/05/07 Javascript
webpack打包html里面img后src为“[object Module]”问题
2019/12/22 Javascript
Nest.js 授权验证的方法示例
2021/02/22 Javascript
Python实现快速排序算法及去重的快速排序的简单示例
2016/06/26 Python
Linux下python与C++使用dlib实现人脸检测
2018/06/29 Python
python 输入一个数n,求n个数求乘或求和的实例
2018/11/13 Python
Python之列表实现栈的工作功能
2019/01/28 Python
Python面向对象程序设计示例小结
2019/01/30 Python
详解python读取image
2019/04/03 Python
python中如何实现将数据分成训练集与测试集的方法
2019/09/13 Python
Python视频编辑库MoviePy的使用
2020/04/01 Python
使用Keras实现Tensor的相乘和相加代码
2020/06/18 Python
详解tensorflow之过拟合问题实战
2020/11/01 Python
html5利用canvas绘画二级树形结构图的示例
2017/09/27 HTML / CSS
日本网路线上商品代购服务:转送JAPAN
2016/08/05 全球购物
婴儿鞋,独特的婴儿服装和配件:Zutano
2018/11/03 全球购物
英国领先的高级美容和在线皮肤诊所:Face the Future
2020/06/17 全球购物
九年级体育教学反思
2014/01/23 职场文书
广告传媒专业应届生求职信
2014/03/01 职场文书
建筑工程技术专业求职信
2014/07/16 职场文书
个人廉政承诺书
2015/04/28 职场文书
情侣之间的道歉短信
2015/05/12 职场文书
公司车队管理制度
2015/08/04 职场文书
浅谈哪个Python库才最适合做数据可视化
2021/06/28 Python
Nginx 502 bad gateway错误解决的九种方案及原因
2022/08/14 Servers