让我们看一段简单的代码:
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()
为什么在这种情况下读取方法像写一样工作?
我使用 Python 2.5 和 2.7 从官方网站下载 Windows。