Search code examples
pythonlinuxunixplist

Editing a plist in python


I have a plist file as follows, that I would like to edit from a python script. I have a setup python script that I want to change the 'InitSetupComplete' value to the string 'YES' after the setup completes. I have already tried a few python snippets i have found on stack overflow, but the plist remains unedited. I am most likely making a mistake in my code. Any ideas on whats going wrong? Do I need to chmod the plist so that the script can edit it?

boot_info.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>InitSetupComplete</key>
    <string>NO</string>
</dict>
</plist>

setup.py

import sys
import plistlib

//Do Setup Here...

//Change Setup Value to YES
pl = plistlib.readPlist("boot_info.plist")
del pl["InitSetupComplete"]
pl["InitSetupComplete"] = { 'InitSetupComplete': 'YES' }
sys.exit()

Solution

  • I think you just need a call to writePlist after you are done editing it. Here is a good article on using this module.