m0ngr31
m0ngr31

Reputation: 1

Editing file header info in Python

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

Upvotes: 0

Views: 424

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798606

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

Upvotes: 1

Related Questions