如何使用Python实现内存映射文件的读写操作?

2026-05-27 03:500阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1081个文字,预计阅读时间需要5分钟。

如何使用Python实现内存映射文件的读写操作?

我这就简化一下这段代码的开头内容:

pythonimport osimport timeimport mmapfilename='test.txt'

我就废话不多说了,还是直接看代码吧!

import os import time import mmap filename = 'test.txt' #如果不存在,创建。 if not os.path.exists(filename): open(filename, 'w') print(os.path.isdir(filename)) if os.path.isfile(filename): print(time.ctime(os.path.getctime(filename))) fd = os.open(filename, os.O_RDWR) m = mmap.mmap(fd, 50, access=mmap.ACCESS_WRITE) # 1024字节的文件。 m.seek(2) buf1 = bytes(b'Zhang') m[2:len(buf1)+2] = buf1 buf2 = b'Phil' m.seek(20) # 定位写入的位置。 m.write(buf2) # 写入字节数据。

阅读全文

本文共计1081个文字,预计阅读时间需要5分钟。

如何使用Python实现内存映射文件的读写操作?

我这就简化一下这段代码的开头内容:

pythonimport osimport timeimport mmapfilename='test.txt'

我就废话不多说了,还是直接看代码吧!

import os import time import mmap filename = 'test.txt' #如果不存在,创建。 if not os.path.exists(filename): open(filename, 'w') print(os.path.isdir(filename)) if os.path.isfile(filename): print(time.ctime(os.path.getctime(filename))) fd = os.open(filename, os.O_RDWR) m = mmap.mmap(fd, 50, access=mmap.ACCESS_WRITE) # 1024字节的文件。 m.seek(2) buf1 = bytes(b'Zhang') m[2:len(buf1)+2] = buf1 buf2 = b'Phil' m.seek(20) # 定位写入的位置。 m.write(buf2) # 写入字节数据。

阅读全文