您的位置:首页 > 编程语言 > Python开发

python 内存映射文件

2012-06-23 10:07 357 查看
可以直接吧内存的对象作为 类 list file 读写赋值啦。

好方便!

eg:

import mmap

# write a simple example file
with open("hello.txt", "wb") as f:
f.write(b"Hello Python!\n")

with open("hello.txt", "r+b") as f:
# memory-map the file, size 0 means whole file
map = mmap.mmap(f.fileno(), 0)
# read content via standard file methods
print(map.readline())  # prints b"Hello Python!\n"
# read content via slice notation
print(map[:5])  # prints b"Hello"
# update content using slice notation;
# note that new content must have same size
map[6:] = b" world!\n"
# ... and read again using standard file methods
map.seek(0)
print(map.readline())  # prints b"Hello  world!\n"
# close the map
map.close()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: