Ken*_*edy 9 python binary file seek
我在将数据附加到二进制文件时遇到问题.当我寻找()到一个位置,然后在该位置写()然后读取整个文件,我发现数据没有写在我想要的位置.相反,我发现它正好在每个其他数据/文本之后.
我的代码
file = open('myfile.dat', 'wb')
file.write('This is a sample')
file.close()
file = open('myfile.dat', 'ab')
file.seek(5)
file.write(' text')
file.close()
file = open('myfile.dat', 'rb')
print file.read()
#prints: This is a sample **text**
Run Code Online (Sandbox Code Playgroud)
你可以看到寻求不起作用.我如何解决这个问题,还有其他方法可以解决这个问题吗?
谢谢
Mar*_*tos 23
在某些系统上,'ab'强制所有写入都发生在文件末尾.你可能想要'r+b'.