[python-win32] Opening existing memory mapped files with pywin32

Tim Roberts timr at probo.com
Tue Feb 16 16:58:21 EST 2021


rhyslloyd1 via python-win32 wrote:
>
> I am trying to open a memory mapped file using Python. I originally 
> used the "mmap" module from Python however I had issues with it 
> because I had to use a fixed size for the file even though my goal was 
> to open an existing file of which the size would be unknown.

The file size isn't really "unknown".  It's just not known in advance.  
There are many ways to find the size of a file.  Plus, the "mmap" module 
accepts 0 as a size parameter, meaning "map the whole file".


> I am now using the "mmapfile.mmapfile" function. My code looks like 
> this <https://pastebin.com/QygT2wp6>. In the docs 
> <http://timgolden.me.uk/pywin32-docs/mmapfile__mmapfile_meth.html> it 
> says I can use a file name of "None" if I'm planning on opening an 
> existing file. This <https://pastebin.com/2FVhpDiB> is the error I get 
> when running my code.

There are a couple of misunderstanding here.  It is important to realize 
that the name of the mapping object is quite different from the name of 
the file being mapped.  The Windows memory-mapping concept is quite 
general, and is often used as a way to share memory between multiple 
processes.  In that case, you just want a chunk of memory without 
bothering with a file on disk.  In that case, you'd create a mapping 
object with a name, but no file name.

If you're just mapping an existing file, you need to supply the file 
name, but you don't need to supply a name for the mapping object.  The 
code you showed creates a shared memory object that does not map a file 
on disk.  The shared memory object has a name (the GUID), and another 
application could open that named object, but it doesn't map to a file.

If you want to open a mapping to an existing file, specify the name of 
the file as File= and specify Name=None.

However, you can do all of this with the mmap module as well.  If you 
have a file called "data.bin", you can do

     import mmap
     fn = open('data.bin','rb')
     data = mmap.mmap( fn.fileno(), 0, access=mmap.ACCESS_READ )

That maps the whole file, and len(data) will tell you how large it is.

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 3389 bytes
Desc: S/MIME Cryptographic Signature
URL: <https://mail.python.org/pipermail/python-win32/attachments/20210216/bf7746b5/attachment.bin>


More information about the python-win32 mailing list