==popen2 模块==

``popen2`` 模块允许你执行外部命令, 并通过流来分别访问它的 ``stdin`` 和 ``stdout`` ( 可能还有 ``stderr`` ). 

在 python 1.5.2 以及之前版本, 该模块只存在于 Unix 平台上. 2.0 后, Windows 下也实现了该函数.
[Example 3-9 #eg-3-9] 展示了如何使用该模块来给字符串排序. ====Example 3-9. 使用 popen2 模块对字符串排序Module to Sort Strings====[eg-3-9] ```
File: popen2-example-1.py import popen2, string fin, fout = popen2.popen2("sort") fout.write("foo\n")
fout.write("bar\n")
fout.close() print fin.readline(),
print fin.readline(),
fin.close() *B*bar
foo*b*
``` [Example 3-10 #eg-3-10] 展示了如何使用该模块控制应用程序 . ====Example 3-10. 使用 popen2 模块控制 gnuchess====[eg-3-10] ```
File: popen2-example-2.py import popen2
import string class Chess:
"Interface class for chesstool-compatible programs" def _ _init_ _(self, engine = "gnuchessc"):
self.fin, self.fout = popen2.popen2(engine)
s = self.fin.readline()
if s != "Chess\n":
raise IOError, "incompatible chess program" def move(self, move):
self.fout.write(move + "\n")
self.fout.flush()
my = self.fin.readline()
if my == "Illegal move":
raise ValueError, "illegal move"
his = self.fin.readline()
return string.split(his)[2] def quit(self):
self.fout.write("quit\n")
self.fout.flush() #
# play a few moves g = Chess() print g.move("a2a4")
print g.move("b2b3") g.quit() *B*b8c6
e7e5*b*
```

  

python标准库介绍——36 popen2 模块详解的更多相关文章

  1. python标准库介绍——27 random 模块详解

    ==random 模块== "Anyone who considers arithmetical methods of producing random digits is, of cour ...

  2. python标准库介绍——12 time 模块详解

    ==time 模块== ``time`` 模块提供了一些处理日期和一天内时间的函数. 它是建立在 C 运行时库的简单封装. 给定的日期和时间可以被表示为浮点型(从参考时间, 通常是 1970.1.1 ...

  3. python标准库介绍——10 sys 模块详解

    ==sys 模块== ``sys`` 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分. === 处理命令行参数=== 在解释器启动后, ``argv`` 列表包含了传递给脚本的所有 ...

  4. python标准库介绍——30 code 模块详解

    ==code 模块== ``code`` 模块提供了一些用于模拟标准交互解释器行为的函数. ``compile_command`` 与内建 ``compile`` 函数行为相似, 但它会通过测试来保证 ...

  5. python标准库介绍——8 operator 模块详解

    ==operator 模块== ``operator`` 模块为 Python 提供了一个 "功能性" 的标准操作符接口. 当使用 ``map`` 以及 ``filter`` 一类 ...

  6. python标准库介绍——4 string模块详解

    ==string 模块== ``string`` 模块提供了一些用于处理字符串类型的函数, 如 [Example 1-51 #eg-1-51] 所示. ====Example 1-51. 使用 str ...

  7. python标准库介绍——33 thread 模块详解

    ?==thread 模块== (可选) ``thread`` 模块提为线程提供了一个低级 (low_level) 的接口, 如 [Example 3-6 #eg-3-6] 所示. 只有你在编译解释器时 ...

  8. python标准库介绍——23 UserString 模块详解

    ==UserString 模块== (2.0 新增) ``UserString`` 模块包含两个类, //UserString// 和 //MutableString// . 前者是对标准字符串类型的 ...

  9. python标准库介绍——22 UserList 模块详解

    ==UserList 模块== ``UserList`` 模块包含了一个可继承的列表类 (事实上是对内建列表类型的 Python 封装). 在 [Example 2-16 #eg-2-16] 中, / ...

随机推荐

  1. C#中使用RabbitMQ收发队列消息

    一.程序使用NetCore.引入Nuget: Install-Package RabbitMQ.Client -Version 4.1.3 二.消息发部端: using RabbitMQ.Client ...

  2. jquery keycode

    判断键盘按下的键 $(window).keydown(function(event) { switch (event.which) { case (13): //Enter //要执行的操作 brea ...

  3. 移动端自适应布局 rem方案

    1.viewport.js (function(window, document) { // 给hotcss开辟个命名空间,别问我为什么,我要给你准备你会用到的方法,免得用到的时候还要自己写. con ...

  4. 快速生成100W条数据

    快速生成100W条数据,生成的时候是顺序生成,取的时候是随机取用,生成100W条数据大概一分多钟,比网上其他代码速度要快很多 )) --truncate table tb --select top 1 ...

  5. java与C#、.NET AES加密、解密 解决方案

      1.情景展示 Java提供的密钥,C#无法解密. 2.原因分析 在Java中,AES的实际密钥需要用到KeyGenerator 和 SecureRandom,但是C#和.NET 里面没有这2个类, ...

  6. Swift3 获取系统音量和监听系统音量

    使用时: //定义滑动条用于显示音量 @IBOutlet weak var volumSlider: UISlider! //处理声音,获取当前音量,并添加监听 handleVolum() 方法内容: ...

  7. [SQL] 请教一下 count里面有case when 一般情况下啥时候用

    http://www.itpub.net/forum.php?mod=viewthread&tid=1810967 问题: 比如 count(case when  pday_id=${deal ...

  8. ThinkPHP学习(二)

    书接前文. 我想前台填写内容,然后在后台保存一下,规划了一下前台要录入的内容,主要包括title.content两大内容.然后简单设计了一个前台页面(复杂的我得会啊),就在上篇文章的基础上直接加了: ...

  9. [Spring学习笔记 2 ]装配各种类型的属性 map,list,array,null,properties

    一.spring Ioc容器补充(1) Spring Ioc容器 DI(依赖注入): 注入的方式:设值方法注入setter(属性注入)/构造子注入(构造函数传入依赖的对象)/字段注入field(注解) ...

  10. lnmp+zabbix 3.2 的编译安装

    yum install pcre* gcc gcc-c++ autoconf automake zlib libxml libjpeg freetype libpng gd curl zlib-dev ...