【发布时间】:2011-12-12 12:35:17
【问题描述】:
让我们看一段简单的代码:
import os
f = open('test.bin', 'wb')
f.write('X')
f.close()
# test.bin - X
f = open('test.bin', 'r+b')
f.seek(0, os.SEEK_END)
f.write('AB')
# test.bin - XAB
f.seek(0, os.SEEK_SET)
f.write('Y')
# test.bin - YAB
print f.read(1)
# test.bin - YBB and prints B 0_o whhyyy?
f.close()
为什么在这种情况下 read 方法像 write 一样工作??
我使用 Python 2.5 和 2.7 for windows 从官方网站下载。
【问题讨论】:
-
在linux上使用python 2.6时,
test.bin末尾包含YAB,最后一次读取返回A。但是,Windows 上的 python 2.6 会导致YBB并且最后一次读取返回B。 -
很奇怪,有什么想法吗?
-
我似乎记得由于
fopen()函数(调用 windows @987654329 @在引擎盖下)。不幸的是,我再也找不到我的源了...最终尝试使用模式'w+b'打开文件。