With respect to two interactions below, I'd expect the same file output by both, but the second one writes at the end of the file. The only difference is a read statement AFTER the write, I don't understand what's happening. What am I missing?
Expected behavior:
>>> f = open("test.txt","w+")
>>> f.write('0123456789')
10
>>> f.seek(0)
0
>>> f.read(3)
'012'
>>> f.seek(0,1)
3
>>> f.write('XX')
2
>>> f.seek(0)
0
>>> f.read()
'012XX56789'
>>> f.close()
Unexpected behavior:
>>> f = open("test.txt","w+")
>>> f.write('0123456789')
10
>>> f.seek(0)
0
>>> f.read(3)
'012'
>>> f.seek(0,1)
3
>>> f.write('XX')
2
>>> f.read(2)
'34'
>>> f.seek(0)
0
>>> f.read()
'0123456789XX'
>>> f.close()
As you can see XX
was written after the whole line, while I was at position 3 when writing these characters.