title: How to seek and append to a binary file in python?
body: <p>I am having problems appending data to a binary file. When i <code>seek()</code> to a location, then <code>write()</code> at that location and then read the whole file, i find that the data was not written at the location that i wanted. Instead, i find it right after every other data/text.</p>
  
  <p>My code</p>
  
  <pre><code>file = open('myfile.dat', 'wb')
  file.write('This is a sample')
  file.close()
  
  file = open('myfile.dat', 'ab')
  file.seek(5)
  file.write(' text')
  file.close()
  
  file = open('myfile.dat', 'rb')
  print file.read()  # -&gt; This is a sample text
  </code></pre>
  
  <p>You can see that the <code>seek</code> does not work. How do i resolve this? are there other ways of achieving this?</p>
  
  <p>Thanks</p>