Python mmap“权限被拒绝”在Linux上
我有一个非常大的文件,我试图用 mmap 打开它,但它给我的权限被拒绝。我已经尝试了 os.open 的不同标志和模式,但它对我不起作用。
我做错了什么?
>>> import os,mmap
>>> mfd = os.open('BigFile', 0)
>>> mfile = mmap.mmap(mfd, 0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
mmap.error: [Errno 13] Permission denied
>>>
(使用内置的 open()
通过 python 文档示例工作,但它似乎在读和写模式下打开多个文件句柄。我需要的 mmap.mmap
方法是文件号,所以我不认为我需要创建一个 file
对象,因此我尝试使用 os.open()
>)
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 working for me.
What am I doing wrong?
>>> import os,mmap
>>> mfd = os.open('BigFile', 0)
>>> mfile = mmap.mmap(mfd, 0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
mmap.error: [Errno 13] Permission denied
>>>
(using the built in open()
works via the python docs example, but it seems to open more than one handle to the file both in read & write mode. All i need for the mmap.mmap
method is the file number, so I wouldn't assume i need to create a file
object; hence my attempt at using os.open()
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为这是一个标志问题,尝试以只读方式打开:
默认情况下,mmap.mmap会尝试映射读/写,因此只需映射只读:
I think its a flags issue, try opening as read only:
and mmap.mmap by default tries to map read/write, so just map read only:
尝试将文件模式设置为
r+
。这在 Linux 上对我有用:然后这对我来说正常:
Try setting the file mode to
r+
. That worked for me on Linux:Then this worked for me as normal:
就我而言,发生此错误是因为我试图打开块设备而不指定显式大小。
FWIW,您不能将 os.stat / os.fstat 与块设备一起使用来获取设备的大小(始终为 0),但您可以使用 file。查找和
file.tell
: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 usefile.seek
andfile.tell
:mmap
的跨平台调用可以通过access
参数进行:mmap构建权限需要与文件打开权限同步(都读取、写入或读/写)。
The cross-platform call of
mmap
can be performed usingaccess
parameter:The mmap construction permissions should be synced with the file open permissions (both read, write, or read/write).