python标准库介绍——19 mmap 模块详解
==mmap 模块== (2.0 新增) ``mmap`` 模块提供了操作系统内存映射函数的接口, 如 [Example 2-13 #eg-2-13] 所示.
映射区域的行为和字符串对象类似, 但数据是直接从文件读取的. ====Example 2-13. 使用 mmap 模块====[eg-2-13] ```
File: mmap-example-1.py import mmap
import os filename = "samples/sample.txt" file = open(filename, "r+")
size = os.path.getsize(filename) data = mmap.mmap(file.fileno(), size) # basics
print data
print len(data), size # use slicing to read from the file
# 使用切片操作读取文件
print repr(data[:10]), repr(data[:10]) # or use the standard file interface
# 或使用标准的文件接口
print repr(data.read(10)), repr(data.read(10)) *B*<mmap object at 008A2A10>
302 302
'We will pe' 'We will pe'
'We will pe' 'rhaps even'*b*
``` 在 Windows 下, 这个文件必须以既可读又可写的模式打开( `r+` , `w+` , 或 `a+` ), 否则 ``mmap`` 调用会失败. ``` [!Feather 注: 经本人测试, a+ 模式是完全可以的, 原文只有 r+ 和 w+] %% WindowsError: [Error 5] Access is denied 一般是这个错误 [Example 2-14 #eg-2-14] 展示了内存映射区域的使用, 在很多地方它都可以替换普通字符串使用,
包括正则表达式和其他字符串操作. ====Example 2-14. 对映射区域使用字符串方法和正则表达式====[eg-2-14] ```
File: mmap-example-2.py import mmap
import os, string, re def mapfile(filename):
file = open(filename, "r+")
size = os.path.getsize(filename)
return mmap.mmap(file.fileno(), size) data = mapfile("samples/sample.txt") # search
index = data.find("small")
print index, repr(data[index-5:index+15]) # regular expressions work too!
m = re.search("small", data)
print m.start(), m.group() *B*43 'only small\015\012modules '
43 small*b*
```
python标准库介绍——19 mmap 模块详解的更多相关文章
- 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标准库介绍——36 popen2 模块详解
==popen2 模块== ``popen2`` 模块允许你执行外部命令, 并通过流来分别访问它的 ``stdin`` 和 ``stdout`` ( 可能还有 ``stderr`` ). 在 pyth ...
- python标准库介绍——23 UserString 模块详解
==UserString 模块== (2.0 新增) ``UserString`` 模块包含两个类, //UserString// 和 //MutableString// . 前者是对标准字符串类型的 ...
- python标准库介绍——22 UserList 模块详解
==UserList 模块== ``UserList`` 模块包含了一个可继承的列表类 (事实上是对内建列表类型的 Python 封装). 在 [Example 2-16 #eg-2-16] 中, / ...
- python标准库介绍——21 UserDict 模块详解
==UserDict 模块== ``UserDict`` 模块包含了一个可继承的字典类 (事实上是对内建字典类型的 Python 封装). [Example 2-15 #eg-2-15] 展示了一个增 ...
随机推荐
- Android -- 拷贝assets下的资源文件到SD卡中(可以超过1M)
很多手机游戏,在安装APK之后都得需要下载相应的资源包,然后才能进入游戏. 有这样一个需求:就是游戏中需要的资源包打在APK内,随apk一起进行安装到手机中. 这样就不需要,在安装APK之后,去下载资 ...
- 【SpringBoot】SpringBoot性能优化
Spring 框架给企业软件开发者提供了常见问题的通用解决方案,包括那些在未来开发中没有意识到的问题.但是,它构建的 J2EE 项目变得越来越臃肿,逐渐被 Spring Boot 所替代.Spring ...
- angular6 NgModule中定义模块module
用这个@NgModule()这个decorator ,放在一个class的上面,这个class一个一个module了 @NgModule() 里面的参数是一个对象,用来配置的,声明这个module里面 ...
- C++的基本类型
- HTML5游戏,五子棋
在线演示 本地下载 最近html5的游戏还真是不少,这种在线游戏既简单又有趣.收藏几个在午休时间娱乐一下.何乐而不为呢?喜欢研究的可以下载代码看看.超级推荐!
- 关于oracle redo log buffer 你所不知道的东西
[ora11@lixora ~]$ sqlplus / as sysdba SQL*Plus: Release 11.2.0.1.0 Production on Wed Oct 8 09:57:50 ...
- VS2010已停止工作,framework 4.0安装失败
好久没在台式机上写程序了,今天打开一个解决方案,报错: 从来没有遇到这种情况,重新关闭解决方案,在菜单栏中打开解决方案说没有framework 4.0,其实我安装的是4.5 ,也有4.0,于是全部卸载 ...
- 微信小程序 - 实现购物车结算
示例源码下载:小程序-实现购物车结算
- 腾讯地图api将物理地址转化成坐标
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- 6、javac命令详解
javac [ options ] [ sourcefiles ] [ @files ] 参数可按任意次序排列. options 命令行选项. sourcefiles 一个或多个要编译的源文件(例如 ...