Increasing a file's size using mmap

有些话、适合烂在心里 提交于 2019-12-23 10:08:08

问题


In Python on Windows I can create a large file by

    from mmap import mmap
    f = open('big.file', 'w')
    f.close()
    f = open('big.file', 'r+')
    m = mmap(f.fileno(), 10**9)

And now big.file is (about) 1 gigabyte. On Linux, though, this will return ValueError: mmap length is greater than file size.

Is there a way to get the same behavior on Linux as with Windows? That is, to be able to increase a file's size using mmap?


回答1:


On POSIX systems at least, mmap() cannot be used to increase (or decrease) the size of a file. mmap()'s function is to memory map a portion of a file. It's logical that the thing you request to map should actually exist! Frankly, I'm really surprised that you would actually be able to do this under MS Windows.

If you want to grow a file, just ftruncate() it before you mmap() it.



来源:https://stackoverflow.com/questions/17393288/increasing-a-files-size-using-mmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!