举例讲解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 tempfile模块学习笔记(临时文件)
May 25 Python
Python实现爬取需要登录的网站完整示例
Aug 19 Python
python 动态加载的实现方法
Dec 22 Python
python读取和保存视频文件
Apr 16 Python
python3.X 抓取火车票信息【修正版】
Jun 19 Python
python使用Matplotlib画饼图
Sep 25 Python
numpy库与pandas库axis=0,axis= 1轴的用法详解
May 27 Python
python中比较两个列表的实例方法
Jul 04 Python
python在OpenCV里实现投影变换效果
Aug 30 Python
python计算波峰波谷值的方法(极值点)
Feb 18 Python
关于Django Models CharField 参数说明
Mar 31 Python
利用python对mysql表做全局模糊搜索并分页实例
Jul 12 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
法国:浪漫之都的咖啡文化
2021/03/03 咖啡文化
通过ICQ网关发送手机短信的PHP源程序
2006/10/09 PHP
在线增减.htpasswd内的用户
2006/10/09 PHP
解析PayPal支付接口的PHP开发方式
2010/11/28 PHP
PHP几个数学计算的内部函数学习整理
2011/08/06 PHP
浅析PHP7新功能及语法变化总结
2016/06/17 PHP
laravel学习教程之关联模型
2016/07/30 PHP
php7基于递归实现删除空文件夹的方法示例
2017/06/15 PHP
Javascript 读后台cookie代码
2008/09/15 Javascript
Javascript之旅 对象的原型链之由来
2010/08/25 Javascript
JS this作用域以及GET传输值过长的问题解决方法
2013/08/06 Javascript
解析prototype,JQuery中跳出each循环的方法
2013/12/12 Javascript
jQuery的$.proxy()应用示例介绍
2014/04/03 Javascript
JavaScript截取字符串的2个函数介绍
2014/08/27 Javascript
一不小心就做错的JS闭包面试题
2015/11/25 Javascript
Node.js的文件权限及读写flag详解
2016/10/11 Javascript
jquery Banner轮播选项卡
2016/12/26 Javascript
jQuery给表格添加分页效果
2017/03/02 Javascript
详解从NodeJS搭建中间层再谈前后端分离
2018/11/13 NodeJs
JavaScript适配器模式原理与用法实例详解
2020/03/09 Javascript
[01:24:34]2014 DOTA2华西杯精英邀请赛5 24 DK VS LGD
2014/05/25 DOTA
[01:11:21]DOTA2-DPC中国联赛 正赛 Phoenix vs CDEC BO3 第三场 3月7日
2021/03/11 DOTA
Python实用日期时间处理方法汇总
2015/05/09 Python
Centos7 Python3下安装scrapy的详细步骤
2018/03/15 Python
Python中sys模块功能与用法实例详解
2020/02/26 Python
pytorch使用horovod多gpu训练的实现
2020/09/09 Python
如何利用python发送邮件
2020/09/26 Python
利用Node实现HTML5离线存储的方法
2020/10/16 HTML / CSS
苹果美国官方商城:Apple美国
2016/08/24 全球购物
授权委托书格式范文
2014/08/02 职场文书
小学生我的梦想演讲稿
2014/08/21 职场文书
机关干部三严三实心得体会
2014/10/13 职场文书
家长意见书
2015/06/04 职场文书
企业文化学习心得体会
2016/01/21 职场文书
你真的了解PHP中的引用符号(&)吗
2021/05/12 PHP
排查并解决MySQL生产库内存使用率高的报警
2022/04/11 MySQL