I would like to update a value in the existing "info.plist" file at runtime with a python script. I am using the plistlib module to achieve it. However I am having an issue while updating an existing value in "Info.plist"
Here is what I've tried so far
import plistlib
try:
p = plistlib.readPlist(filePath)
newValue = { "ApplicationID" : "com.test.abc"}
if "ApplicationID" in p:
p["ApplicationID"].extend(newValue)
else:
p["ApplicationID"] = newValue
plistlib.writePlist(p, filePath)
except:
print "Error plist file"
If the application id does not exist then this code should add an entry to the plist file but if it already exists in Info.plist, I should get an exception and "Error plist file" should be printed.
Can you please help me diagnose what's wrong in this code?
Env Using:
Python Version: 2.7
MacOSX
OK, after re-reading your question, I think you just want to do this:
import plistlib
p = plistlib.readPlist(filePath)
if "ApplicationID" in p:
print "Error plist file"
else:
p["ApplicationID"] = "com.test.abc"
plistlib.writePlist(p, filePath)
This will work as long as your plist is already in XML format. If you really wanted to raise an exception, you could do that after or instead of the print statement.
--- Edit in response to comment ---
Well, that's what I had originally expected from the code, but your description below that said you wanted an exception if it already existed, so I thought I was wrong :)
In that case you only need to do this:
import plistlib
try:
p = plistlib.readPlist(filePath)
p["ApplicationID"] = "com.test.abc"
plistlib.writePlist(p, filePath)
except:
print("Oh no! Failure :(")