Why do these two very similar codes behave so differently?
Code 1:
with open("new.txt", 'w+') as f:
f.write("this is a line")
with open("new.txt", 'r+') as f:
f.write("***")
f.read() # << note here
f.seek(0)
print(f.read())
Output: ***s is a line
Code 2:
with open("new.txt", 'w+') as f:
f.write("this is a line")
with open("new.txt", 'r+') as f:
f.write("***")
f.read(1) # << note here
f.seek(0)
print(f.read())
Output: this is a line***
Python version: Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug 1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)] on win32
Python 3.11.0 (main, Nov 15 2022, 05:43:36) [Clang 14.0.0 (clang-1400.0.29.202)] on darwin)Python 3.9.6 (default, Sep 24 2021, 19:09:16) [GCC 7.4.0] on linux. It seems to be platform independent.iomodule, so until the bug is fixed you can work around it by using the Python implementation of theiomodule instead by addingfrom _pyio import open.