举例讲解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中的条件判断语句与循环语句用法小结
Mar 21 Python
Python常用库推荐
Dec 04 Python
Python复数属性和方法运算操作示例
Jul 21 Python
详解如何利用Cython为Python代码加速
Jan 27 Python
PyQt5实现拖放功能
Apr 25 Python
浅谈Django中的数据库模型类-models.py(一对一的关系)
May 30 Python
pyqt5让图片自适应QLabel大小上以及移除已显示的图片方法
Jun 21 Python
python opencv minAreaRect 生成最小外接矩形的方法
Jul 01 Python
Django实现web端tailf日志文件功能及实例详解
Jul 28 Python
Django admin.py 在修改/添加表单界面显示额外字段的方法
Aug 22 Python
python 实现查询Neo4j多节点的多层关系
Dec 23 Python
Python批量将图片灰度化的实现代码
Apr 11 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中iconv函数使用方法
2008/05/24 PHP
PHP 与 UTF-8 的最佳实践详细介绍
2017/01/04 PHP
Laravel5.1 框架表单验证操作实例详解
2020/01/07 PHP
PHP日期和时间函数的使用示例详解
2020/08/06 PHP
CL vs ForZe BO5 第四场 2.13
2021/03/10 DOTA
jQuery 获取URL参数的插件
2010/03/04 Javascript
Jquery 分页插件之Jquery Pagination
2015/08/25 Javascript
jQuery实用技巧必备(上)
2015/11/02 Javascript
js实现的页面加载完毕之前loading提示效果完整示例【附demo源码下载】
2016/08/02 Javascript
AngulerJS学习之按需动态加载文件
2017/02/13 Javascript
Agularjs妙用双向数据绑定实现手风琴效果
2017/05/26 Javascript
JavaScript之promise_动力节点Java学院整理
2017/07/03 Javascript
Vue组件实例间的直接访问实现代码
2017/08/20 Javascript
基于vue.js无缝滚动效果
2018/01/25 Javascript
总结JavaScript在IE9之前版本中内存泄露问题
2018/04/28 Javascript
Vue导出页面为PDF格式的实现思路
2018/07/31 Javascript
使用react context 实现vue插槽slot功能
2019/07/18 Javascript
vue+elementUI实现图片上传功能
2019/08/20 Javascript
解决layui数据表格table的横向滚动条显示问题
2019/09/04 Javascript
python转换字符串为摩尔斯电码的方法
2015/07/06 Python
详解python使用递归、尾递归、循环三种方式实现斐波那契数列
2018/01/16 Python
matplotlib.pyplot绘图显示控制方法
2019/01/15 Python
python基础知识(一)变量与简单数据类型详解
2019/04/17 Python
pytorch 在sequential中使用view来reshape的例子
2019/08/20 Python
Pytorch 计算误判率,计算准确率,计算召回率的例子
2020/01/18 Python
python中threading开启关闭线程操作
2020/05/02 Python
英国高街品牌:Miss Selfridge(塞尔弗里奇小姐)
2016/09/21 全球购物
存储过程的优点有哪些
2012/09/27 面试题
工程造价管理专业大专生求职信
2013/10/06 职场文书
市场营销职业生涯规划书范文
2014/01/12 职场文书
结婚邀请函范文
2014/01/14 职场文书
班级学习雷锋活动总结
2014/07/04 职场文书
纪检干部个人对照检查材料
2014/09/23 职场文书
授权委托书范本(单位)
2014/09/28 职场文书
穆斯林的葬礼读书笔记
2015/06/26 职场文书
React Native项目框架搭建的一些心得体会
2021/05/28 Javascript