In python, there are a few flags you can supply when opening a file for operation. I am a bit baffled at finding a combination that allow me to do random write without truncating. The behavior I am looking for is equivalent to C: create it if it doesn't exist, otherwise, open for write (not truncating)
with open("test.txt", "a+") as f:
You should open in
with open("file", "rb+") as file: file.write(b"...")
On Python 2 you could use
r+ instead for text mode, but you shouldn't since it can change the length of the text you write.