==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. Linux常用命令之wget

    wget:从网络上下载文件到当前目录.

  2. Win7安装golang开发环境--备忘录

    首次接触golang,非常多东西不熟悉,Git也没有安装. 1 安装golang 1.1 下载golang 我是在Golang中国的下载频道下载的golang1.6.链接例如以下: http://ww ...

  3. AppScan--图解Web扫描工具IBM Security App Scan Standard

    App Scan用法:   首先打开IBM Security AppScan Standard 工具   点击 创建新的扫描 ->  点击”常规扫描“ ->之后你就会看到如下图:     ...

  4. js Object.create 初探

    1.作用 Object.create()方法创建一个新对象,使用现有的对象来提供新创建的对象的__proto__. https://developer.mozilla.org/zh-CN/docs/W ...

  5. 解决 ERROR in native method: JDWP No transports initialized, jvmtiError=AGENT_ERROR_TRANSPORT_INIT(197)

    在/etc/hosts文件中加入下面一行内容 127.0.0.1 localhost.localdomain localhost

  6. BIEE11G配置Oracle 12c数据源

    方法一:直接在连接池中输入连接信息 将tnsname.ora里配置的连接信息等号后面的字符串去掉空格和换行符即可) (DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)( ...

  7. git reset与git revert比較

    git reset git reset的作用是还原Index的状态或改动本地分支HEAD的位置. 这个命令适合用于从某个提交点又一次开新的分支.比方,假设某个提交之后的代码我们都不要了,就能够在本地r ...

  8. Selenium+TestNG+Jenkins 框架图形化UML表示

  9. V-rep学习笔记:机器人模型创建1—模型简化

    要进行机器人仿真首先需要得到机器人的几何模型.我们可以直接通过VREP中提供的基本几何体来搭建一个简易的机器人[Menu bar --> Add --> Primitive shape - ...

  10. CTRL+SHIFT

    CTRL+SHIFT+鼠标左右,上下拖动,可快速实现平行和垂直上下复制的功能,