19

I am having problems appending data to a binary file. When i seek() to a location, then write() 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.

My 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()  # -> This is a sample text

You can see that the seek does not work. How do i resolve this? are there other ways of achieving this?

Thanks

1
  • 1
    Note the binary mode isn't important. You can reproduce the same issue with text mode. Commented Dec 28, 2019 at 16:59

4 Answers 4

37

On some systems, 'ab' forces all writes to happen at the end of the file. You probably want 'r+b'.

Sign up to request clarification or add additional context in comments.

4 Comments

From the docs for the seek method: "If the file is only opened for writing in append mode (mode 'a'), this method is essentially a no-op,"
@bgporter: Are you supporting or refuting something I said? I honestly can't tell.
Sorry -- supporting! Just adding the actual text from the docs, trying to clarify (but obviously failing..)
The non-intuitive part thing is that the docs say 'r+' opens the file for both reading and updating -- with the latter implying being able to write.
6

r+b should work as you wish

Comments

3

Leave out the seek command. You already opened the file for append with 'a'.

3 Comments

Judging from the sample code, I don't think the OP wants to append to the end of the file.
You may be correct but i assumed the result should be 'This is a sample text'. :)
I think this should be the accepted answer. 'r+b' only works for OP because of the seek command.
-1

NOTE : Remember new bytes over write previous bytes

As per python 3 syntax

with open('myfile.dat', 'wb') as file:
    b = bytearray(b'This is a sample')
    file.write(b)

with open('myfile.dat', 'rb+') as file:
    file.seek(5)
    b1 = bytearray(b'  text')
    #remember new bytes over write previous bytes
    file.write(b1)

with open('myfile.dat', 'rb') as file:
    print(file.read())

OUTPUT

b'This   textample'

remember new bytes over write previous bytes

3 Comments

so, how can we wb those two variables into a file and read them?
Do you mean writing them on different lines? in that case, you can use file.seek(len(b)) the second time. OR best way is to write both variables one after the other when you open file in writing mode. Then close the file. And next time open file in reading mode and read all contents
it doesn't have to be on a different line, I have posted a question about this. stackoverflow.com/questions/60795901/…. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.