7

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.

Community
  • 1
  • 1
Jeremie
  • 290
  • 3
  • 10
  • `mmap()`ing a file will not change the size of the file. If you want to `mmap()` 1e7 bytes of a file, you'll need to do something beforehand to make sure that the file is actually at least that large... – twalberg Apr 15 '15 at 16:13
  • I had considered that, but understood there had to be a way to fill in the data into a pre-allocated amount of space set aside for mmap() rather than starting with a file the intended size. – Jeremie Apr 15 '15 at 17:02

1 Answers1

4

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.

Community
  • 1
  • 1
zom-pro
  • 1,571
  • 2
  • 16
  • 32