举例讲解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编程实现及时获取新邮件的方法示例
Aug 10 Python
解决pycharm无法识别本地site-packages的问题
Oct 13 Python
django框架使用方法详解
Jul 18 Python
Python爬虫实现使用beautifulSoup4爬取名言网功能案例
Sep 15 Python
Python3 实现爬取网站下所有URL方式
Jan 16 Python
python入门之基础语法学习笔记
Feb 08 Python
Python线程threading模块用法详解
Feb 26 Python
python爬虫实例之获取动漫截图
May 31 Python
Python Tkinter图形工具使用方法及实例解析
Jun 15 Python
python利用递归方法实现求集合的幂集
Sep 07 Python
python 实现的IP 存活扫描脚本
Dec 10 Python
python中使用asyncio实现异步IO实例分析
Feb 26 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短网址的生成代码(仿微博短网址)
2014/05/07 PHP
php去掉URL网址中带有PHPSESSID的配置方法
2014/07/08 PHP
javascript parseInt 函数分析(转)
2009/03/21 Javascript
jquery 模式对话框终极版实现代码
2009/09/28 Javascript
Javascript学习笔记9 prototype封装继承
2010/01/11 Javascript
一个网马的tips实现分析
2010/11/28 Javascript
JavaScript中的关键字"VAR"使用详解 分享
2013/07/31 Javascript
javascript进行四舍五入方法汇总
2014/12/16 Javascript
扒一扒JavaScript 预解释
2015/01/28 Javascript
深入剖析javascript中的exec与match方法
2016/05/18 Javascript
Node.js环境下JavaScript实现单链表与双链表结构
2016/06/12 Javascript
jQuery layui常用方法介绍
2016/07/25 Javascript
基于jQuery实现滚动刷新效果
2017/01/09 Javascript
Vue.js结合bootstrap实现分页控件
2017/03/10 Javascript
react高阶组件经典应用之权限控制详解
2017/09/07 Javascript
Vue2.0基于vue-cli+webpack Vuex的用法(实例讲解)
2017/09/15 Javascript
Vue拖拽组件开发实例详解
2018/05/11 Javascript
使用vue打包时vendor文件过大或者是app.js文件很大的问题
2018/06/29 Javascript
VUE 配置vue-devtools调试工具及安装方法
2018/09/30 Javascript
vue中eslintrc.js配置最详细介绍
2018/12/21 Javascript
使用 Github Actions 自动部署 Angular 应用到 Github Pages的方法
2020/07/20 Javascript
Python编程使用*解包和itertools.product()求笛卡尔积的方法
2017/12/18 Python
python实现整数的二进制循环移位
2019/03/08 Python
Python 实现数据结构-循环队列的操作方法
2019/07/17 Python
python使用梯度下降和牛顿法寻找Rosenbrock函数最小值实例
2020/04/02 Python
Python基于paramunittest模块实现excl参数化
2020/04/26 Python
pytorch掉坑记录:model.eval的作用说明
2020/06/23 Python
超30万乐谱下载:Musicnotes.com
2016/09/24 全球购物
四年级语文教学反思
2014/02/05 职场文书
元旦寄语大全
2014/04/10 职场文书
三好学生演讲稿范文
2014/04/26 职场文书
超市开业庆典策划方案
2014/05/14 职场文书
2014年教师批评与自我批评思想汇报
2014/09/20 职场文书
会计工作态度自我评价
2015/03/06 职场文书
2015高中教师个人工作总结
2015/07/21 职场文书
导游词之西递宏村
2019/12/10 职场文书