举例讲解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中正则的使用指南
Dec 04 Python
Python下实现的RSA加密/解密及签名/验证功能示例
Jul 17 Python
Python列表和元组的定义与使用操作示例
Jul 26 Python
Python实现读取TXT文件数据并存进内置数据库SQLite3的方法
Aug 08 Python
Python元组及文件核心对象类型详解
Feb 11 Python
Python3.遍历某文件夹提取特定文件名的实例
Apr 26 Python
python编辑用户登入界面的实现代码
Jul 16 Python
python框架中flask知识点总结
Aug 17 Python
基于Python的微信机器人开发 微信登录和获取好友列表实现解析
Aug 21 Python
将keras的h5模型转换为tensorflow的pb模型操作
May 25 Python
Python Http请求json解析库用法解析
Nov 28 Python
python实现会员管理系统
Mar 18 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中遍历stdclass object的实现代码
2011/06/09 PHP
PHP数组 为文章加关键字连接 文章内容自动加链接
2011/12/29 PHP
JoshChen_php新手进阶高手不可或缺的规范介绍
2013/08/16 PHP
EarthLiveSharp中cloudinary的CDN图片缓存自动清理python脚本
2017/04/04 PHP
PHP对象的浅复制与深复制的实例详解
2017/10/26 PHP
PHPTree――php快速生成无限级分类
2018/03/30 PHP
document.open() 与 document.write()的区别
2007/08/13 Javascript
计算新浪Weibo消息长度(还可以输入119字)
2013/07/02 Javascript
用jQuery与JSONP轻松解决跨域访问的问题
2014/02/04 Javascript
ExtJs动态生成treepanel的Json格式
2015/07/19 Javascript
js基于setTimeout与setInterval实现多线程
2016/06/17 Javascript
AngularJS 多指令Scope问题的解决
2018/10/25 Javascript
如何去除富文本中的html标签及vue、react、微信小程序中的过滤器
2018/11/21 Javascript
图文讲解vue的v-if使用方法
2019/02/11 Javascript
微信小程序实现下拉刷新动画
2019/06/21 Javascript
jQuery删除/清空指定元素的所有子节点实例代码
2019/07/04 jQuery
vue3为什么要用proxy替代defineProperty
2020/10/19 Javascript
复习Python中的字符串知识点
2015/04/14 Python
ubuntu环境下python虚拟环境的安装过程
2018/01/07 Python
python 3.7.0 下pillow安装方法
2018/08/27 Python
用Python编写一个简单的CS架构后门的方法
2018/11/20 Python
Python实现分段线性插值
2018/12/17 Python
python3 pygame实现接小球游戏
2019/05/14 Python
Python list与NumPy array 区分详解
2019/11/06 Python
几款Python编译器比较与推荐(小结)
2020/10/15 Python
django中cookiecutter的使用教程
2020/12/03 Python
英国独特的时尚和生活方式品牌:JOY
2018/03/17 全球购物
特色冷饮店创业计划书
2014/01/28 职场文书
《哪吒闹海》教学反思
2014/02/28 职场文书
毕业晚会主持词
2014/03/24 职场文书
竞选演讲稿范文大全
2014/05/12 职场文书
银行青年文明号事迹材料
2014/05/31 职场文书
小学生国庆节演讲稿
2014/09/05 职场文书
转学证明范本
2015/06/19 职场文书
导游词之昭君岛
2020/01/17 职场文书
vue.js Router中嵌套路由的实用示例
2021/06/27 Vue.js