本文主要是介绍mmap创建超过既定文件大小的memory-mapped file,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
-
问题
-
解决方案
The unix version of mmap doesn’t grow the file automatically, but can just write zeros to the file by yourself:
size = 1073741824 # bytes in 1GiB self.file = open("/path/demo.csv", "w+b") self.file.write("\0" * size) self.file.flush() self.mm = mmap.mmap(self.file.fileno(), size, access= mmap.ACCESS_WRITE)
But this takes too long to finish, it can be improved by :
with open("file.to.create", "wb") as out:out.seek((1024 * 1024 * 1024) - 1)out.write('\0')
-
Reference
- Why can’t I create a new file with mmap of known size?
- create file of particular size in python
这篇关于mmap创建超过既定文件大小的memory-mapped file的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!