举例讲解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 12 Python
Windows下使Python2.x版本的解释器与3.x共存的方法
Oct 25 Python
Android应用开发中Action bar编写的入门教程
Feb 26 Python
简单了解什么是神经网络
Dec 23 Python
python排序函数sort()与sorted()的区别
Sep 18 Python
Python 实现异步调用函数的示例讲解
Oct 14 Python
python内置数据类型之列表操作
Nov 12 Python
对python内置map和six.moves.map的区别详解
Dec 19 Python
python钉钉机器人运维脚本监控实例
Feb 20 Python
Python将主机名转换为IP地址的方法
Aug 14 Python
Pytorch 多维数组运算过程的索引处理方式
Dec 27 Python
Python中常见的导入方式总结
May 06 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
ezSQL PHP数据库操作类库
2010/05/16 PHP
PHP实现的mysql主从数据库状态检测功能示例
2017/07/20 PHP
Thinkphp 框架扩展之数据库驱动常用方法小结
2020/04/23 PHP
鼠标移动到一张图片时变为另一张图片
2006/12/05 Javascript
jQuery 点击图片跳转上一张或下一张功能的实现代码
2010/03/12 Javascript
基于jquery的分页控件(C#)
2011/01/06 Javascript
JS+CSS实现可拖拽的漂亮圆角特效弹出层完整实例
2015/02/13 Javascript
js显示动态时间的方法详解
2016/08/20 Javascript
手机浏览器 后退按钮强制刷新页面方法总结
2016/10/09 Javascript
微信小程序图表插件(wx-charts)实例代码
2017/01/17 Javascript
js仿QQ邮箱收件人选择与搜索功能
2017/02/10 Javascript
解决Vue.js 2.0 有时双向绑定img src属性失败的问题
2018/03/14 Javascript
解决iView中时间控件选择的时间总是少一天的问题
2018/03/15 Javascript
vue自定义tap指令及tap事件的实现
2018/09/18 Javascript
如何搭建一个完整的Vue3.0+ts的项目步骤
2020/10/18 Javascript
在vue项目中封装echarts的步骤
2020/12/25 Vue.js
[01:48:04]DOTA2-DPC中国联赛 正赛 PSG.LGD vs Elephant BO3 第一场 2月7日
2021/03/11 DOTA
python计算最大优先级队列实例
2013/12/18 Python
python开发之字符串string操作方法实例详解
2015/11/12 Python
Python实现针对中文排序的方法
2017/05/09 Python
tensorflow 1.0用CNN进行图像分类
2018/04/15 Python
python操作excel的包(openpyxl、xlsxwriter)
2018/06/11 Python
基于Python开发chrome插件的方法分析
2018/07/07 Python
使用python验证代理ip是否可用的实现方法
2018/07/25 Python
django 获取字段最大值,最新的记录操作
2020/08/09 Python
python利用google翻译方法实例(翻译字幕文件)
2020/09/21 Python
详解基于Scrapy的IP代理池搭建
2020/09/29 Python
奥地利顶级内衣丝袜品牌英国站:Wolford英国
2016/08/29 全球购物
关于旷工的检讨书
2014/02/02 职场文书
公司新年寄语
2014/04/04 职场文书
学校端午节活动方案
2014/08/23 职场文书
四风个人对照检查材料思想汇报(办公室通用版)
2014/10/07 职场文书
南京导游词
2015/02/03 职场文书
应届毕业生自荐信
2015/03/04 职场文书
2015年党总支工作总结
2015/05/25 职场文书
2016年庆“七一”主题党日活动总结
2016/04/05 职场文书