python标准库介绍——36 popen2 模块详解
==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 模块详解的更多相关文章
- python标准库介绍——27 random 模块详解
==random 模块== "Anyone who considers arithmetical methods of producing random digits is, of cour ...
- python标准库介绍——12 time 模块详解
==time 模块== ``time`` 模块提供了一些处理日期和一天内时间的函数. 它是建立在 C 运行时库的简单封装. 给定的日期和时间可以被表示为浮点型(从参考时间, 通常是 1970.1.1 ...
- python标准库介绍——10 sys 模块详解
==sys 模块== ``sys`` 模块提供了许多函数和变量来处理 Python 运行时环境的不同部分. === 处理命令行参数=== 在解释器启动后, ``argv`` 列表包含了传递给脚本的所有 ...
- python标准库介绍——30 code 模块详解
==code 模块== ``code`` 模块提供了一些用于模拟标准交互解释器行为的函数. ``compile_command`` 与内建 ``compile`` 函数行为相似, 但它会通过测试来保证 ...
- python标准库介绍——8 operator 模块详解
==operator 模块== ``operator`` 模块为 Python 提供了一个 "功能性" 的标准操作符接口. 当使用 ``map`` 以及 ``filter`` 一类 ...
- python标准库介绍——4 string模块详解
==string 模块== ``string`` 模块提供了一些用于处理字符串类型的函数, 如 [Example 1-51 #eg-1-51] 所示. ====Example 1-51. 使用 str ...
- python标准库介绍——33 thread 模块详解
?==thread 模块== (可选) ``thread`` 模块提为线程提供了一个低级 (low_level) 的接口, 如 [Example 3-6 #eg-3-6] 所示. 只有你在编译解释器时 ...
- python标准库介绍——23 UserString 模块详解
==UserString 模块== (2.0 新增) ``UserString`` 模块包含两个类, //UserString// 和 //MutableString// . 前者是对标准字符串类型的 ...
- python标准库介绍——22 UserList 模块详解
==UserList 模块== ``UserList`` 模块包含了一个可继承的列表类 (事实上是对内建列表类型的 Python 封装). 在 [Example 2-16 #eg-2-16] 中, / ...
随机推荐
- scala VS python2 操作shell对比例子
Scala: /** * Created by sunxu on 2015/9/30. */ import scala.sys.process._ import java.io.File //在相应目 ...
- 禁用系统的Ctrl+Alt+Left/Right(方向键)
对于非常多工具,如IntelliJ IDE,Ctrl+Alt+Left/Right(方向键)是一个非常重要的快捷键,可是这个快捷键经常会被一些显示相关的附属应用给占用用于调整屏幕显示的朝向,有时候即使 ...
- 《Cocos2d-JS开发之旅》重印在即,感谢大家的支持
3月第一次印刷的<Cocos2d-JS开发之旅>已经销售完毕,即将启动第二次印刷. 感谢各位读者的支持,最近<开发之旅>荣登京东cocos2d-x系列书籍的销售排行首位. 新版 ...
- createjs入门
createjs是一个轻量级的框架,稍微有点时间和耐心,就可以把全部源代码都看一遍,毕竟只有三十几个js文件.地址:http://www.createjs.com/ 开发createjs的动画或游戏, ...
- 【DB2】清理日志文件db2diag.log
在数据库运行过程中,会产生很多的日志,进而使日志文件db2diag.log变大 当占用磁盘空间太大的时候,应该怎么处理呢? 方法一: db2inst1@Linux:/db2home/db2inst1/ ...
- 转 php 5.3.6中php-fpm 配置
php 5.3.6 中 php-fpm 配置 成 服务启动 从php5.3.3开始 源码中开始包含 php-fpm,不用专门再打补丁了,只需要解开源码直接configure, 关于php-fpm的编译 ...
- timerTask任务定时器
简单的一个定时器,异步任务 Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void ru ...
- (原)ubuntu挂载及开机自动挂载网络端的文件夹的方法
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/7160792.html 参考网址: http://blog.csdn.net/tlight/articl ...
- [转载]将archlinux 2013-06-01版,安装配置为个人工作站
原文地址:将archlinux 2013-06-01版,安装配置为个人工作站作者:老圃无蔓 安装所使用的镜像为:archlinux-2013.06.01-dual.iso.首先请看看我安装完成之后的效 ...
- git difftool 详解
一.如何比较两个版本之间的差异 1.显示版本得到版本的commit id 2.执行difftool命令 按Y进行比较,我用的是DiffMerge这个软件对代码进行比较的 二.比较当前所修改的内容 gi ...