举例讲解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下调用pytesseract识别某网站验证码的实现方法
Jun 06 Python
Python中动态检测编码chardet的使用教程
Jul 06 Python
Python爬虫框架Scrapy基本用法入门教程
Jul 26 Python
Pandas的read_csv函数参数分析详解
Jul 02 Python
python 判断三个数字中的最大值实例代码
Jul 24 Python
django中media媒体路径设置的步骤
Nov 15 Python
基于python调用psutil模块过程解析
Dec 20 Python
pycharm运行程序时看不到任何结果显示的解决
Feb 21 Python
Python3爬虫带上cookie的实例代码
Jul 28 Python
Python+unittest+requests 接口自动化测试框架搭建教程
Oct 09 Python
python开发一款翻译工具
Oct 10 Python
OpenCV-Python实现人脸磨皮算法
Jun 07 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
声音就能俘获人心,蕾姆,是哪个漂亮小姐姐配音呢?
2020/03/03 日漫
Linux下php5.4启动脚本
2014/08/03 PHP
window.open()弹出居中的窗口
2007/02/01 Javascript
js字符编码函数区别分析
2008/06/05 Javascript
javascript的解析执行顺序在各个浏览器中的不同
2014/03/17 Javascript
javascript实现瀑布流自适应遇到的问题及解决方案
2015/01/28 Javascript
jQuery实现带有上下控制按钮的简单多行滚屏效果代码
2015/09/04 Javascript
js基础知识(公有方法、私有方法、特权方法)
2015/11/06 Javascript
jquery获取下拉框中的循环值
2017/02/08 Javascript
bootstrap如何让dropdown menu按钮式下拉框长度一致
2017/04/10 Javascript
node.js微信小程序配置消息推送的实现
2019/02/13 Javascript
React Hooks 实现和由来以及解决的问题详解
2020/01/17 Javascript
JavaScript中作用域链的概念及用途讲解
2020/08/06 Javascript
微信小程序自定义底部弹出框功能
2020/11/18 Javascript
[01:29:46]DOTA2上海特级锦标赛C组资格赛#1 OG VS LGD第二局
2016/02/27 DOTA
[01:16:13]DOTA2-DPC中国联赛 正赛 SAG vs Dragon BO3 第一场 2月22日
2021/03/11 DOTA
使用Python编写一个模仿CPU工作的程序
2015/04/16 Python
Python定时执行之Timer用法示例
2015/05/27 Python
Python基于numpy灵活定义神经网络结构的方法
2017/08/19 Python
Python搜索引擎实现原理和方法
2017/11/27 Python
PyTorch学习笔记之回归实战
2018/05/28 Python
Pytorch 中retain_graph的用法详解
2020/01/07 Python
Python3使用tesserocr识别字母数字验证码的实现
2021/01/29 Python
Staples美国官方网站:办公用品一站式采购
2016/07/28 全球购物
英国太阳镜品牌:Taylor Morris Eyewear
2018/04/18 全球购物
美国美食礼品篮网站:Gourmet Gift Baskets
2019/12/15 全球购物
中专生自我鉴定书范文
2013/12/28 职场文书
优秀民警事迹材料
2014/01/29 职场文书
高中军训感言200字
2014/02/23 职场文书
元宵晚会主持词
2014/03/25 职场文书
机电一体化应届生求职信
2014/08/09 职场文书
期末复习计划
2015/01/19 职场文书
学会感恩主题班会
2015/08/12 职场文书
九年级化学教学反思
2016/02/22 职场文书
自定义函数实现单词排序并运用于PostgreSQL(实现代码)
2021/04/22 PostgreSQL
python实现简单的井字棋
2021/05/26 Python