Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mmap file inquiry for a blank file in Python

Tags:

python

mmap

I'm having some problems getting a file to memory map, and was hoping to resolve this issue. I've detailed the problem and shown my code below.

What I'm importing:

import os
import mmap

Now, for the code:

file = r'otest'                         # our file name
if os.path.isfile(file):                # test to see if the file exists
os.remove(file)                     # if it does, delete it
f = open(file, 'wb')                    # Creates our empty file to write to
print(os.getcwd())

Here is where I encounter the problem with my code (I've included both, and have one commented out each time I run the program):

mfile = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_WRITE)
#mfile = mmap.mmap(f.fileno(), 10**7, access=mmap.ACCESS_WRITE)

I'm encountering an error with either of the mfile lines. For the mmap.mmap line with the 0 argument I get this error: ValueError: cannot mmap an empty file. If I instead use the 10**7 argument I get this error instead: PermissionError: [WinError 5] Access is denied

And to end it:

"""
    Other stuff goes here
"""
f.close()                               # close out the file

The 'other stuff here' is just a place holder for a where I'm going to put more code to do things.

Just to add, I've found this thread which I thought may help, but both the ftruncate and os.truncate functions did not seem to help the issue at hand.

like image 258
Jeremie Avatar asked Apr 15 '15 15:04

Jeremie


1 Answers

As that thread that you linked shows, mmap requires you to create the file first and then modify it. So first, create an empty file doing something like:

f = open(FILENAME, "wb")
f.write(FILESIZE*b'\0')
f.close()

Then, you will be able to access the file and mapping it using:

f = open(FILENAME, "r+b")
mapf = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_WRITE)

Notice the way the file is being open. Remember that you can flush your buffer by doing (more details here Usage of sys.stdout.flush() method):

sys.stdout.flush()

Let me know if you want me to go into details about any of this points.

like image 194
zom-pro Avatar answered Nov 10 '22 22:11

zom-pro