Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python mmap 'Permission denied' on Linux

Tags:

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())

like image 959
tMC Avatar asked Jun 08 '11 23:06

tMC


People also ask

How do I fix Python permission denied error?

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 .

What is mmap Python?

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.

How do I fix errno 13 Permission denied in Python?

We can fix this error by ensuring by closing a file after performing an i/o operation on the file.


2 Answers

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) 
like image 88
Bobby Powers Avatar answered Sep 17 '22 13:09

Bobby Powers


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) 
like image 28
Chris Laplante Avatar answered Sep 18 '22 13:09

Chris Laplante