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()
)
To fix PermissionError: [Errno 13] Permission denied with Python open, we should make sure the path we call open with is a file. to make sure that the path is a path to a file with os. path. isfile before we call open to open the file at the path .
Python's mmap provides memory-mapped file input and output (I/O). It allows you to take advantage of lower-level operating system functionality to read files as if they were one large string or array. This can provide significant performance improvements in code that requires a lot of file I/O.
We can fix this error by ensuring by closing a file after performing an i/o operation on the file.
I think its a flags issue, try opening as read only:
mfd = os.open('BigFile', os.O_RDONLY)
and mmap.mmap by default tries to map read/write, so just map read only:
mfile = mmap.mmap(mfd, 0, prot=mmap.PROT_READ)
Try setting the file mode to r+
. That worked for me on Linux:
mfd = os.open('BigFile', "r+")
Then this worked for me as normal:
mfile = mmap.mmap(mfd, 0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With