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] 中, / ...
随机推荐
- Linux安装pear包
一.安装pear包. 1.安装: $ sudo wget http://pear.php.net/go-pear.phar $ sudo php go-pear.har 2.查看pear下安装的包: ...
- http 三次握手
1.三次握手 目的:主要规避由于网络阻塞,传输不畅等原因导致服务器端端口一直被占用,无法释放端口资源.(想想假设只要一次就建立连接,服务器响应后浏览器若是没收到数据,等待一段时间后,网络超时,浏览器重 ...
- Git 推送和删除远程标签
事实上Git 的推送和删除远程标签命令是相同的,删除操作实际上就是推送空的源标签refs: git push origin 标签名 相当于 git push origin refs/tags/源标签名 ...
- Windows虚拟内存不足问题的处理
一般而言,当系统缺少虚拟内存会引起很多问题,包含但不限于: 操作系统运行缓慢,甚至出现Hang(夯机),蓝色背景界面的情况 用户本地控制台输入用户名.密码后,无法登录到操作系统,卡在欢迎界面 无法远程 ...
- 系统安装-007 CentOS7yum源添加、删除及其yum优化(转)
一.配置阿里云源为主源mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bakwget -O /et ...
- 理解Android编译命令(转)
一.引言 关于Android Build系统,这个话题很早就打算整理下,迟迟没有下笔,决定跟大家分享下.先看下面几条指令,相信编译过Android源码的人都再熟悉不过的. source setenv. ...
- .NET 工具生成引擎概述
Mark Michaelis 微软中国MSDN 过去几年大家一直都在使用 .NET Core(有这么久吗?)并且都知道“生成系统”经历了重大改变,不论是终止对 Gulp 的内置支持,还是放弃 Proj ...
- java 数组声明方法
//数组 public class Test16{ public static void main(String args[]){ //声明一: int [] x; x = new int[3];// ...
- window搭建python环境
在window开发python代码,搭建python环境! 01.下载python-win https://www.python.org/downloads/windows/ http://ipyth ...
- Spring MVC测试框架详解——服务端测试
随着RESTful Web Service的流行,测试对外的Service是否满足期望也变的必要的.从Spring 3.2开始Spring了Spring Web测试框架,如果版本低于3.2,请使用sp ...