0

I'm pretty new to python, and I'm trying to edit data in a .mobi file from a Python script. I am able to read all the meta data, but I can't figure out how to save the info I need back into the correct place. Every time I try, it just corrupts the file.

This is a snippet of what I'm having problems with:

    metadata['ASIN'] = "B00012345"
    data += "Modified ASIN: " + metadata['ASIN']

    g = file(infile)
    header2 = g.read(312)

    f = open(infile, 'wb')
    f.seek(header2[0xA4E])
    f.write(metadata['ASIN'])
    f.close()

Can someone tell me what I'm doing wrong?

Thanks

1
  • What exactly are you trying to do to the file, are you trying to append some data onto it? Are you trying to insert or overwrite a few bytes in the middle? Commented Jul 14, 2011 at 22:57

1 Answer 1

1

wb (and w) truncates the file on opening. You want rb+.

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

Comments

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.