开发者

Editing file header info in Python

I'm 开发者_如何学JAVApretty 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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜