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 相关文章推荐
pycharm 使用心得(一)安装和首次使用
Jun 05 Python
不要用强制方法杀掉python线程
Feb 26 Python
Python实现Youku视频批量下载功能
Mar 14 Python
Python标准库之collections包的使用教程
Apr 27 Python
Python 爬虫之超链接 url中含有中文出错及解决办法
Aug 03 Python
详解Python之unittest单元测试代码
Jan 24 Python
python列表生成式与列表生成器的使用
Feb 23 Python
使用python将时间转换为指定的格式方法
Nov 12 Python
python实现控制电脑鼠标和键盘,登录QQ的方法示例
Jul 06 Python
一篇文章搞定Python操作文件与目录
Aug 13 Python
flask利用flask-wtf验证上传的文件的方法
Jan 17 Python
详解使用scrapy进行模拟登陆三种方式
Feb 21 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
玩家交还《星际争霸》原始码光盘 暴雪报以厚礼
2017/05/05 星际争霸
php 在windows下配置虚拟目录的方法介绍
2013/06/26 PHP
php将csv文件导入到mysql数据库的方法
2014/12/24 PHP
PHP载入图像imagecreatefrom_gif_jpeg_png系列函数用法分析
2016/11/14 PHP
PHP dirname简单使用代码实例
2020/11/13 PHP
JavaScript面向对象编程
2008/03/02 Javascript
jquery在IE、FF浏览器的差别详细探讨
2013/04/28 Javascript
IE下JS保存图片的简单实例
2016/07/15 Javascript
AngularJS基础 ng-repeat 指令简单示例
2016/08/03 Javascript
angularJS Provider、factory、service详解及实例代码
2016/09/21 Javascript
js实现本地图片文件拖拽效果
2017/07/18 Javascript
微信小程序中setInterval的使用方法
2017/09/29 Javascript
JavaScript闭包与作用域链实例分析
2019/01/21 Javascript
Vue插件之滑动验证码
2019/09/21 Javascript
[33:33]完美世界DOTA2联赛PWL S2 FTD.C vs SZ 第二场 11.27
2020/11/30 DOTA
[01:04:08]完美世界DOTA2联赛PWL S3 INK ICE vs GXR 第一场 12.16
2020/12/18 DOTA
使用python实现正则匹配检索远端FTP目录下的文件
2015/03/25 Python
浅谈django model的get和filter方法的区别(必看篇)
2017/05/23 Python
浅谈python和C语言混编的几种方式(推荐)
2017/09/27 Python
numpy中以文本的方式存储以及读取数据方法
2018/06/04 Python
django基础学习之send_mail功能
2019/08/07 Python
Python远程开发环境部署与调试过程图解
2019/12/09 Python
python 消费 kafka 数据教程
2019/12/21 Python
解决tensorflow由于未初始化变量而导致的错误问题
2020/01/06 Python
Pandas对DataFrame单列/多列进行运算(map, apply, transform, agg)
2020/06/14 Python
Algenist奥杰尼官网:微藻抗衰老护肤品牌
2017/07/15 全球购物
全球第二大家装零售商:Lowe’s
2018/01/13 全球购物
经济实惠的豪华家具:My-Furniture
2019/03/12 全球购物
Java里面Pass by value和Pass by Reference是什么意思
2016/05/02 面试题
Java程序员面试题
2016/09/27 面试题
见习期自我鉴定
2013/11/07 职场文书
幼儿园元旦亲子活动方案
2014/02/17 职场文书
民主生活会对照检查材料(统计局)
2014/09/21 职场文书
办理信用卡工作证明
2014/09/30 职场文书
论群众路线学习心得体会
2014/10/31 职场文书
新闻稿格式范文
2015/07/18 职场文书