==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中磁盘分区、格式化、挂载等管理

    1. 添加磁盘,查看磁盘状况 [root@db1 /]# fdisk -l Disk /dev/sda: bytes heads, sectors/track, cylinders Units = c ...

  2. SqlServer和Mysql插入记录前判断是否存在,存在则插入,不存在则修改。

    SqlServer中是这样: ) ,@title,@searchKeys,@serviceIntervalSecond,@sleepMillisecondPerSearch) ELSE UPDATE ...

  3. linux2.6.30.4内核移植(6)——移植应用程序hello world常见的错误:-bin/sh ./hello not found

    通常在开发板上搭建好开发平台后,我们会试着移植一个最简单的应用程序Hello world来测试一下.初次尝试,我们经常会碰到的问题就是,在开发板上运行./hello的时候出错:-bin/sh ./he ...

  4. 用Eclipse进行远程Debug代码 (转)

    来自:http://blog.csdn.net/fyq891014/article/details/7534711 首先你本地Eclipse上要有和部署在远程服务器一至的项目,否则debug的时候会出 ...

  5. Java之创建对象>3.Enforce the singleton property with a private constructor or an enum type

     1. 通过一个公开的字段来获取单例 // Singleton with public final field public class Elvis { public static final Elv ...

  6. Selenium简单测试页面加载速度的性能(Page loading performance)

    利用selenium的可以执行javascript脚本的特性,我写了一个java版本的获得页面加载速度的代码,这样你就可以在进行功能测试的同时进行一个简单的测试页面的加载速度的性能测试. 我现在的项目 ...

  7. JUC-ReadWriteLock

    ReadWriteLock 维护了一对相关的锁,一个用于只读操作,另一个用于写入操作.只要没有 writer,读取锁可以由多个 reader 线程同时保持.写入锁是独占的. ReadWriteLock ...

  8. C#并行编程-PLINQ:声明式数据并行-转载

    C#并行编程-PLINQ:声明式数据并行   目录 C#并行编程-相关概念 C#并行编程-Parallel C#并行编程-Task C#并行编程-并发集合 C#并行编程-线程同步原语 C#并行编程-P ...

  9. 微信扫码支付springboot版本

    发布时间:2018-11-06   技术:springboot+freemarker   概述 该项目是一个采用springboot构建的web项目,主要实现了微信扫码支付功能.包含最基本的创建订单, ...

  10. Asp.net中GridView使用详解(引)【转】

    Asp.net中GridView使用详解(引) GridView无代码分页排序 GridView选中,编辑,取消,删除 GridView正反双向排序 GridView和下拉菜单DropDownList ...