See Python's excellent online docs, specifically (what I'm pointing you to) those for the built-in function open
:
Modes 'r+', 'w+' and 'a+' open the
file for updating (note that 'w+'
truncates the file).
Since you want to read the existing file you don't want to truncate it, and since you want to write over it at a specific byte offset you don't want the append-update mode either, so it should be pretty obvious which of the three "open for updating" modes you want to use, right? Just pass it as the second argument to open
(the first one being the file's name or path).
The open
function gives you a file
object, on which you can call the seek method to change the position for the next read or write, and tell
(documented right after it at the URL I just pointed you to) if you need to know the current position at any time.