举例讲解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并发编程之多进程、多线程、异步和协程详解
Oct 28 Python
python 链接和操作 memcache方法
Mar 04 Python
对numpy中轴与维度的理解
Apr 18 Python
Python 实现字符串中指定位置插入一个字符
May 02 Python
python 两个数据库postgresql对比
Oct 21 Python
PyQT5 emit 和 connect的用法详解
Dec 13 Python
python将音频进行变速的操作方法
Apr 08 Python
python+adb+monkey实现Rom稳定性测试详解
Apr 23 Python
Python实现初始化不同的变量类型为空值
Jun 02 Python
解决Keras TensorFlow 混编中 trainable=False设置无效问题
Jun 28 Python
Django正则URL匹配实现流程解析
Nov 13 Python
Python编解码问题及文本文件处理方法详解
Jun 20 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
全国FM电台频率大全 - 16 河南省
2020/03/11 无线电
jquery tools之tooltip
2009/07/25 Javascript
jquery插件 cluetip 关键词注释
2010/01/12 Javascript
Javascript中Eval函数的使用
2010/03/23 Javascript
使用JQuery快速实现Tab的AJAX动态载入(实例讲解)
2013/12/11 Javascript
返回顶部按钮响应滚动且动态显示与隐藏
2014/10/14 Javascript
CSS javascript 结合实现悬浮固定菜单效果
2015/08/23 Javascript
基于jquery实现在线选座订座之影院篇
2015/08/24 Javascript
详解JavaScript 中的 replace 方法
2016/01/01 Javascript
快速掌握Node.js中setTimeout和setInterval的使用方法
2016/03/21 Javascript
jQuery EasyUI编辑DataGrid用combobox实现多级联动
2016/08/29 Javascript
微信小程序 自定义消息提示框
2017/08/06 Javascript
Mongoose实现虚拟字段查询的方法详解
2017/08/15 Javascript
深入理解React高阶组件
2017/09/28 Javascript
jquery实现回车键触发事件(实例讲解)
2017/11/21 jQuery
jQuery实现DIV响应鼠标滑过由下向上展开效果示例【测试可用】
2018/04/26 jQuery
Vue实现本地购物车功能
2018/12/05 Javascript
记一次vue-webpack项目优化实践详解
2019/02/17 Javascript
微信小程序学习笔记之函数定义、页面渲染图文详解
2019/03/28 Javascript
python访问sqlserver示例
2014/02/10 Python
windows及linux环境下永久修改pip镜像源的方法
2016/11/28 Python
Python命令启动Web服务器实例详解
2017/02/23 Python
基于Python3 逗号代码 和 字符图网格(详谈)
2017/06/22 Python
Pipenv一键搭建python虚拟环境的方法
2018/05/22 Python
Linux下远程连接Jupyter+pyspark部署教程
2019/06/21 Python
澳大利亚玩具剧场:Toy Playhouse
2019/03/03 全球购物
美国台面电器和厨具品牌:KitchenAid
2019/04/12 全球购物
Audible英国:有声读物,30天免费试用
2019/10/16 全球购物
Seavenger官网:潜水服、浮潜、靴子和袜子
2020/03/05 全球购物
七年级英语教学反思
2014/01/15 职场文书
纪念九一八事变演讲稿:牢记历史,捍卫主权
2014/09/14 职场文书
2014年加油站站长工作总结
2014/12/23 职场文书
财务管理制度范本
2015/08/04 职场文书
珍爱生命主题班会
2015/08/13 职场文书
聊聊redis-dump工具安装问题
2022/01/18 Redis
Python数据处理的三个实用技巧分享
2022/04/01 Python