Python plistlib on OS X appending instead of overwriting

409 views Asked by At

Apologies for non-idiomatic Python; corrections on that welcome, but that's not my primary issue.

I am using plistlib to update keys in the the Info.plist file inside an application bundle. As far as I know, from the Python perspective, an application bundle is just a directory named "something.app". I'm having an issue, though, where calling plistlib.load(fp) followed by updating keys and then plistlib.dump(plist, fp) is appending the entire plist file's XML contents on the existing file, so that I end up with a file that has two entire XML trees inside it.

This is the code:

    with open(plistPath, 'r+b') as fp:
       plistRoot = plistlib.load(fp)
       plistRoot["CFBundleIdentifier"] = newBundleId
       plistlib.dump(plistRoot, fp)

I suppose I can always close the file context, blow away the file, and then open a new file with the same name and write to that, but it doesn't seem like that should be necessary.

1

There are 1 answers

1
Alice Isabelle On BEST ANSWER

Resolved by re-opening the file with a second with-as statement for writing.