The mmap module

The mmap module

(New in 2.0) This module provides an interface to the operating system’s memory mapping functions. The mapped region behaves pretty much like a string object, but data is read directly from the file.

Example: Using the mmap module
# 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))
$ python mmap-example-1.py
<mmap object at 008A2A10>
302 302
'We will pe' 'We will pe'
'We will pe' 'rhaps even'

Under Windows, the file must currently be opened for both reading and writing (r+, or w+), or the mmap call will fail.

Memory mapped regions can be used instead of ordinary strings in many places, including regular expressions and many string operations:

Example: Using string functions and regular expressions on a mapped region
# 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()
$ python mmap-example-2.py
43 'only small\015\012modules '
43 small

The mmap module的更多相关文章

  1. Modules you should know in Python Libray

    前两天被问到常用的python lib和module有哪些?最常用的那几个,其他的一下子竟然回答不上.想想也是,一般情况下,遇到一个问题,在网上一搜,顺着线索找到可用的例子,然后基本没有怎么深究.结果 ...

  2. PHP APC缓存配置、使用详解

    一.APC缓存简介 APC,全称是Alternative PHP Cache,官方翻译叫”可选PHP缓存”.它为我们提供了缓存和优化PHP的中间代码的框架. APC的缓存分两部分:系统缓存和用户数据缓 ...

  3. xlrd doc

    The xlrd Module A Python module for extracting data from MS Excel ™ spreadsheet files. Version 0.7.3 ...

  4. 查看python中包的文档

    核心命令:python -m pydoc 查询某包:python -m pydoc 包名 示例: C:\Users\xxx>python -m pydoc pydoc - the Python ...

  5. Perl语言入门:第九章 使用正则表达式处理文本 示例程序和代码

    #! /usr/bin/perl use strict; use warnings; print "\n----------------------------------_substitu ...

  6. Python之mmap内存映射模块(大文本处理)说明

    背景: 通常在UNIX下面处理文本文件的方法是sed.awk等shell命令,对于处理大文件受CPU,IO等因素影响,对服务器也有一定的压力.关于sed的说明可以看了解sed的工作原理,本文将介绍通过 ...

  7. Linux内存映射(mmap)系列(1)

    看到同事的代码中出现了mmap.所以自己私下学习学习,研究研究..... http://www.cnblogs.com/lknlfy/archive/2012/04/27/2473804.html ( ...

  8. 【转】Python之mmap内存映射模块(大文本处理)说明

    [转]Python之mmap内存映射模块(大文本处理)说明 背景: 通常在UNIX下面处理文本文件的方法是sed.awk等shell命令,对于处理大文件受CPU,IO等因素影响,对服务器也有一定的压力 ...

  9. 内核模块module传参

    linux 2.6允许用户insmod的时候往内核模块里面传递参数,它主要使用module_param宏定义来实现这一功能. 参数应用 module_param(name, type, perm); ...

随机推荐

  1. cocos2d-x3.2中加入Android手机震动

    本人宣布从此博文发出后,我的cocos2dx的引擎从cocos2dx3.1.1跳到cocos2dx3.2,哈哈,事实上变化不大的,不碍事~~~ 以下来说说在cocos中加入Android手机震动的功能 ...

  2. GCC 编译使用动态链接库和静态链接库的方法

    1 库的分类 依据链接时期的不同,库又有静态库和动态库之分. 静态库是在链接阶段被链接的.所以生成的可执行文件就不受库的影响了.即使库被删除了,程序依旧能够成功执行. 有别于静态库,动态库的链接是在程 ...

  3. OCP-1Z0-051-题目解析-第8题

    8. View the Exhibit and examine the structure of the CUSTOMERS table. Which two tasks would require ...

  4. c语言,数据类型转换

    在执行算术运算时,计算机比C语言的限制更多.为了让计算机执行算术运算,通常要求操作数有相同的大小(即位的数量相同),并且要求存储的方式也相同.计算机可能可以直接将两个16位整数相加,但是不能直接将16 ...

  5. BZOJ 3446: [Usaco2014 Feb]Cow Decathlon( 状压dp )

    水状压dp. dp(x, s) = max{ dp( x - 1, s - {h} ) } + 奖励(假如拿到的) (h∈s). 时间复杂度O(n * 2^n) ------------------- ...

  6. android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 3

           今天在写一个小项目的数据库部分的功能时,出现了一个这样的问题:java.lang.RuntimeException: Failure delivering result ResultIn ...

  7. word2vec 中的数学原理具体解释(一)文件夹和前言

      word2vec 是 Google 于 2013 年开源推出的一个用于获取 word vector 的工具包.它简单.高效.因此引起了非常多人的关注. 因为 word2vec 的作者 Tomas ...

  8. Qt 多线程 详细函数说明及其事例

    转:http://www.cnblogs.com/hicjiajia/archive/2011/02/03/1948955.html Qt线程类 Qt 包含下面一些线程相关的类:QThread 提供了 ...

  9. 按钮的图标 Button icons-JQUERY MOBILE 1.0正式版中文手册

    按钮的图标 Button icons-JQUERY MOBILE 1.0正式版中文手册 data-icon属性可以被用来创建如下所示的图标 左箭头data-icon="arrow-l&quo ...

  10. HTML5 input placeholder 颜色 改动

    David Murdoch:Chrome支持input=[type=text]占位文本属性,但下列CSS样式却不起作用: CSS input[placeholder], [placeholder], ...