Reputation: 1741
This simple python code:
import mmap
with file("o:/temp/mmap.test", "w+b") as fp:
m = mmap.mmap(fp.fileno(), 0, access=mmap.ACCESS_READ|mmap.ACCESS_WRITE)
m.write("Hello world!")
Produces the following error (on the mmap.mmap(...) line):
WindowsError: [Error 1006] The volume for a file has been externally altered so that the opened file is no longer valid
Any idea why?
Upvotes: 1
Views: 3901
Reputation: 3
FYI - with python 2.7.6
dataFile = open(dFile, mode='r+b') # failed with windows access error
dataFile = open(dFile, 'r+b') # works
reading r+b, to get around \x1a SUB characters in the data, seen as end of file
Upvotes: 0
Reputation: 21089
Most likely because w+
truncates the file, and Windows gives an error when trying to create an empty mapping from that file of length 0. Use r+
instead.
As well, you shouldn't use access=mmap.ACCESS_READ|mmap.ACCESS_WRITE
:
>>> mmap.ACCESS_READ
1
>>> mmap.ACCESS_WRITE
2
>>> mmap.ACCESS_COPY
3
>>> mmap.ACCESS_READ | mmap.ACCESS_WRITE
3
In other words, access=mmap.ACCESS_READ|mmap.ACCESS_WRITE
is the same as access=mmap.ACCESS_COPY
. What you want is most likely access=mmap.ACCESS_WRITE
, and on Windows that's what you get anyway if you don't explicitly use that argument.
Try this:
import mmap
with file("o:/temp/mmap.test", "r+b") as fp:
m = mmap.mmap(fp.fileno(), 0)
m.write("Hello world!")
( mmap docs: http://docs.python.org/library/mmap.html )
Upvotes: 4
Reputation: 18318
From the documentation:
If length is 0, the maximum length of the map is the current size of the file, except that if the file is empty Windows raises an exception (you cannot create an empty mapping on Windows).
You are opening the file with "w+" - the file is getting truncated... (size = 0)
Upvotes: 4