Request mmap file for empty file in Python

I had problems getting the file on the memory card, and I was hoping to solve this problem. I described the problem in detail and showed my code below.

What I import:

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

This is where I ran into a problem with my code (I included both and commented each time every 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 encounter an error with any of the mfile lines. To line mmap.mmapwith the argument 0I get this error: ValueError: cannot mmap an empty file. If I use an argument instead 10**7, I get this error:PermissionError: [WinError 5] Access is denied

And for this:

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

“Other stuff here” is simply the owner of the place where I am going to add more code.

, , , , , ftruncate os.truncate, , .

+4
1

, , mmap , , . , , - :

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

:

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

. , ( sys.stdout.flush():

sys.stdout.flush()

, , .

+1

All Articles