尝试在 Windows 下写入 mmap 时出现奇怪的错误
这个简单的 python 代码:
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!")
产生以下错误(在 mmap.mmap(...) 行上):
WindowsError:[错误 1006] 文件的卷已被外部更改,因此打开的文件不再有效
知道为什么吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自文档:
您正在使用“w+”打开文件 - 文件正在被截断...(大小 = 0)
From the documentation:
You are opening the file with "w+" - the file is getting truncated... (size = 0)
最有可能的原因是
w+
截断了文件,并且 Windows 在尝试从长度为 0 的文件创建空映射时给出错误。请改用r+
。同样,您不应该使用
access=mmap.ACCESS_READ|mmap.ACCESS_WRITE
:换句话说,
access=mmap.ACCESS_READ|mmap.ACCESS_WRITE
与 <代码>access=mmap.ACCESS_COPY。您想要的很可能是access=mmap.ACCESS_WRITE
,在 Windows 上,如果您不明确使用该参数,那么您无论如何都会得到它。试试这个:(
mmap 文档: http://docs.python.org/library/mmap.html )
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. User+
instead.As well, you shouldn't use
access=mmap.ACCESS_READ|mmap.ACCESS_WRITE
:In other words,
access=mmap.ACCESS_READ|mmap.ACCESS_WRITE
is the same asaccess=mmap.ACCESS_COPY
. What you want is most likelyaccess=mmap.ACCESS_WRITE
, and on Windows that's what you get anyway if you don't explicitly use that argument.Try this:
( mmap docs: http://docs.python.org/library/mmap.html )
仅供参考 - 使用 python 2.7.6
读取 r+b,以绕过数据中的 \x1a SUB 字符,被视为文件结尾
FYI - with python 2.7.6
reading r+b, to get around \x1a SUB characters in the data, seen as end of file