我正在嘗試使用python修改plist文件。我從我的圖書館複製了一個plist到我的桌面上以安全地播放它。我輸入os
和plistlib
。我遵循我在documentation here中看到的內容。plistlib無法讀取Safari的plist文件
import plistlib
import os
test_prefs = "~/Desktop/com.apple.Safari.plist"
x = readPlist(os.path.expanduser(test_prefs))
print x["TopSitesGridArrangement"]
但是這個失敗。我究竟做錯了什麼?
確切的錯誤,我得到:
Traceback (most recent call last):
File "/Users/Josh/Desktop/destroy.py", line 11, in <module>
x = readPlist(os.path.expanduser(test_prefs))
NameError: name 'readPlist' is not defined
當我將其更改爲x = plistlib.readPlist(os.path.expanduser(test_prefs))
我得到的是如下的錯誤(我的文件名字叫做destroy.py):
Traceback (most recent call last):
File "/Users/Josh/Desktop/destroy.py", line 11, in <module>
x = plistlib.readPlist(os.path.expanduser(test_prefs))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plistlib.py", line 78, in readPlist
rootObject = p.parse(pathOrFile)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plistlib.py", line 406, in parse
parser.ParseFile(fileobj)
xml.parsers.expat.ExpatError: not well-formed (invalid token): line 1, column 8
這不應該給你報告的錯誤。你可以[編輯]你的問題,包括**完整的追溯**? – mhlester
而不是'x = readPlist(...'你試過'x = plistlib.readPlist(...'? – fredtantini
@mhlester你是對的,我用真正的錯誤信息更新了它 – wetjosh