举例讲解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中处理XML的教程
Apr 29 Python
python中assert用法实例分析
Apr 30 Python
在Python中操作字典之clear()方法的使用
May 21 Python
python+selenium实现登录账户后自动点击的示例
Dec 22 Python
django query模块
Apr 20 Python
python opencv实现图像边缘检测
Apr 29 Python
Python实现的爬取百度贴吧图片功能完整示例
May 10 Python
解决tensorflow添加ptb库的问题
Feb 10 Python
Python爬虫实现模拟点击动态页面
Mar 05 Python
深入了解Python 变量作用域
Jul 24 Python
python进度条显示之tqmd模块
Aug 22 Python
python切片作为占位符使用实例讲解
Feb 17 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
php下实现伪 url 的超简单方法[转]
2007/09/24 PHP
php循环检测目录是否存在并创建(循环创建目录)
2011/01/06 PHP
PHP中call_user_func_array回调函数的用法示例
2016/11/26 PHP
PHP开发中csrf攻击的简单演示和防范
2017/05/07 PHP
实用的Jquery选项卡TAB示例代码
2013/08/28 Javascript
JQuery中层次选择器用法实例详解
2015/05/18 Javascript
JS动态添加iframe的代码
2015/09/14 Javascript
js+canvas简单绘制圆圈的方法
2016/01/28 Javascript
js添加绑定事件的方法
2016/05/15 Javascript
js实现精确到秒的倒计时效果
2016/05/29 Javascript
JS检测页面中哪个HTML标签触发点击事件的方法
2016/06/17 Javascript
js实现刷新页面后回到记录时滚动条的位置【两种方案可选】
2016/12/12 Javascript
微信小程序实现图片自适应(支持多图)
2017/01/25 Javascript
jQuery实现移动端Tab选项卡效果
2017/03/15 Javascript
bootstrap suggest下拉框使用详解
2017/04/10 Javascript
AngularJS使用$http配置对象方式与服务端交互方法
2018/08/13 Javascript
vue最简单的前后端交互示例详解
2018/10/11 Javascript
[02:41]《西雅图我们来了》2015国际邀请赛出征全记录
2015/07/23 DOTA
[53:15]Newbee vs Pain 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
[01:03:18]DOTA2-DPC中国联赛 正赛 RNG vs Dynasty BO3 第一场 1月29日
2021/03/11 DOTA
线程和进程的区别及Python代码实例
2015/02/04 Python
利用Python实现简单的相似图片搜索的教程
2015/04/23 Python
python快速建立超简单的web服务器的实现方法
2018/02/17 Python
使用python实现mqtt的发布和订阅
2019/05/05 Python
python 实现生成均匀分布的点
2019/12/05 Python
Anaconda+Pycharm环境下的PyTorch配置方法
2020/03/13 Python
实例讲解使用SVG制作loading加载动画的方法
2016/04/05 HTML / CSS
英国No.1文具和办公用品在线:Euroffice
2016/09/21 全球购物
英国儿童图书网站:Scholastic
2017/03/26 全球购物
《我为你骄傲》教学反思
2014/02/20 职场文书
促销活动总结
2014/04/28 职场文书
说谎欺骗人检讨书300字
2014/11/18 职场文书
2014年销售工作总结与计划
2014/12/01 职场文书
庆七一活动简报
2015/07/20 职场文书
高中班主任工作总结(范文)
2019/08/20 职场文书
Linux中各个目录的作用与内容
2022/06/28 Servers