Plist文件解析错误“'str'对象没有属性'read'”

时间:2016-05-06 12:16:25

标签: python plist osx-yosemite

我正在尝试使用plistlib在Mac OSX上读取.plist文件。 可悲的是,我在运行脚本时总是遇到错误

Traceback (most recent call last):
  File "/Users/johannes/pycharmprojects/adobe-cache-cleaner/test.py", line 6, in <module>
    pl = plistlib.load(fp2)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plistlib.py", line 983, in load
    header = fp.read(32)

AttributeError: 'str' object has no attribute 'read'

那是我的剧本:

import plistlib

fp2 = "/Users/Johannes/Pythonproject/test.plist"

pl = plistlib.load(fp2)
print pl

1 个答案:

答案 0 :(得分:1)

看起来ptlistlib期待文件不是字符串:

import plistlib

with open("/Users/Johannes/Pythonproject/test.plist", "rb") as file:
    pl = plistlib.load(file)
    print pl

请参阅https://docs.python.org/3/library/plistlib.html