When the "r+", "w+", or "a+" access type is specified, both reading and writing are
allowed (the file is said to be open for "update"). However, when you switch between
reading and writing, there must be an intervening fflush, fsetpos, fseek, or rewind
operation. The current position can be specified for the fsetpos or fseek operation,
if desired.
使用seek方法后,不报错了。但是还是不能把结果write进去。
f = open('test.txt','r+')
text= f.read()
print text
f.seek(0)
f.write('haha')
f.close()