Python mmap 'Permission denied' on Linux

后端 未结 4 1097
梦毁少年i
梦毁少年i 2021-02-18 14:26

I have a really large file I\'m trying to open with mmap and its giving me permission denied. I\'ve tried different flags and modes to the os.open but its just not

4条回答
  •  悲哀的现实
    2021-02-18 14:44

    In my case this error occurred because I was attempting to open a block device without specifying an explicit size.

    FWIW you cannot use os.stat / os.fstat with a block device to obtain the device's size (which is always 0), but you can use file.seek and file.tell:

    f = file("/dev/loop0", "rb")
    f.seek(0, 2)  # Seek relative to end of file
    size = f.tell()
    fh = f.fileno()
    
    m = mmap.mmap(f, size, mmap.MAP_PRIVATE, mmap.PROT_READ)
    

提交回复
热议问题